ewok
ewok

Reputation: 21503

vim: custom highlighting for log files

I have log files that take the following form:

YYYY-MM-DD HH:MM:SS.sss SEVERITY [thread.name] class.name:line -

SEVERITY is one of DEBUG, INFO, WARN, or ERROR.

I have a syntax file set up that uniquely highlights the timestamp and each of the different severities. Now, however, I want to highlight the thread name, and the class name and line number. the issue is that there may be square brackets within the log messages, so simply starting at +\[+ and ending on +\]+ wouldn't work.

I'd love to put down what I've tried but honestly I just have not idea how to do it. Here's the file I have so far:

:syn region logTime start=+^\d\{4}-\d\{2}-\d\{2} \d\{2}:\d\{2}:\d\{2}+ end=+ +me=e-1
:syn region logError start=+ERROR+ end=+ +me=e-1
:syn region logWarn start=+WARN+ end=+ +me=e-1
:syn region logInfo start=+INFO+ end=+ +me=e-1
:syn region logDebug start=+DEBUG+ end=+ +me=e-1

hi def logError ctermfg=white ctermbg=red
hi def logWarn ctermfg=yellow
hi def logInfo ctermfg=green
hi def logDebug ctermfg=blue
hi def logTime ctermfg=white ctermbg=blue

Upvotes: 4

Views: 2108

Answers (1)

Ingo Karkat
Ingo Karkat

Reputation: 172738

First, you're overusing the :syntax region; for the severities, a much simpler (and faster!) :syn keyword is enough:

:syn keyword logInfo INFO

As the thread name comes after the severity (after whitespace), you can instruct Vim to specifically attempt to parse those afterwards:

:syn keyword logInfo INFO skipwhite nextgroup=logThreadName

To avoid that the bracketed thread name is parsed elsewhere, specify the contained parameter:

:syn match logThreadName "\[\S\+\]" contained

Again, a :syn match is simpler than a region.

Upvotes: 6

Related Questions