Thursday, April 26, 2007

Vim syntax highlighting for log files

If you are a "vim" user and looking for a quick way to do"syntax highlighting" for log files, created using "log4j", try this. The following was used in Vim 7.0.

Just make sure the log priority level("%p") is present in the log output.

# ~/.vim/filetype.vim should have at least the following.

if exists("did_load_filetypes")
finish
endif

augroup filetypedetect
" log files such as catalina.out or log4j files.
au! BufRead,BufNewFile catalina.out,*.out,*.out.*,*.log,*.log.* setf log
augroup END

# ~/.vim/syntax/log.vim - create this file

" Vim syntax file
" file type: log files
" Quit when a (custom) syntax file was already loaded
if exists("b:current_syntax")
finish
endif
syn match fatal ".* FATAL .*"
syn match fatal "^FATAL: .*"
syn match error ".* ERROR .*"
syn match error "^ERROR: .*"
syn match warn ".* WARN .*"
syn match warn "^WARN: .*"
syn match info ".* INFO .*"
syn match info "^INFO: .*"
syn match debug ".* DEBUG .*"
syn match debug "^DEBUG: .*"

syn match error "^java.*Exception.*"
syn match error "^java.*Error.*"
syn match error "^\tat .*"

" Highlight colors for log levels.
hi fatal ctermfg=Red ctermbg=Black
hi error ctermfg=Red ctermbg=Black
hi warn ctermfg=Yellow ctermbg=Black
hi info ctermfg=Green ctermbg=Black
hi debug ctermfg=Gray ctermbg=Black

let b:current_syntax = "log"

" vim: ts=2 sw=2

No comments: