我正在尝试为日志文件编写自定义语法高亮显示。 每个日志行都以明确定义的标题开头,例如:
TRACE: text text text
DEBUG: text text text
TRACE和DEBUG行将有不同的颜色,例如:
:syn match logTrace "^\d\{4}-\d\{2}-\d\{2} \d\{2}:\d\{2}:\d\{2}|TRACE|.*"
:syn match logDebug "^\d\{4}-\d\{2}-\d\{2} \d\{2}:\d\{2}:\d\{2}|DEBUG|.*"
hi def logTrace ctermfg=darkgray
hi def logDebug ctermfg=lightgray
问题是可能存在日志条目跨越多行的情况,例如:
TRACE: Here's a list of files:
file1.ext
file2.ext
file3.ext
DEBUG: text text text
因此,我的问题是:有没有办法告诉语法高亮引擎继续着色未上网的行(例如上例中的file1.ext),其颜色与上次检测到的行中使用的颜色相同(TRACE行中的上面的例子)?
谢谢。
上面的例子是一个简化的案例。我的日志实际上也包含INFO,ERROR,EXCEPTION和FATAL日志类型,每行都以时间戳开头。 日志行看起来像这样:
2013-04-11 13:36:05|TRACE|texttexttexttexttext
2013-04-11 13:36:07|INFO|texttexttexttexttexttext
another line
another line
2013-04-11 13:36:10|DEBUG|texttexttexttext
2013-04-11 13:36:10|ERROR|texttexttexttexttexttext
another line
another line
another line
2013-04-11 13:36:10|DEBUG|texttexttexttext
我最终实现了Ineo的区域理念。以下是它的外观:
:syn region logTrace start=+^\d\{4}-\d\{2}-\d\{2} \d\{2}:\d\{2}:\d\{2}|TRACE|+ end=+^\d\{4}-\d\{2}-\d\{2} \d\{2}:\d\{2}:\d\{2}|+me=e-22
:syn region logDebug start=+^\d\{4}-\d\{2}-\d\{2} \d\{2}:\d\{2}:\d\{2}|DEBUG|+ end=+^\d\{4}-\d\{2}-\d\{2} \d\{2}:\d\{2}:\d\{2}|+me=e-22
:syn region logInfo start=+^\d\{4}-\d\{2}-\d\{2} \d\{2}:\d\{2}:\d\{2}|INFO|+ end=+^\d\{4}-\d\{2}-\d\{2} \d\{2}:\d\{2}:\d\{2}|+me=e-22
:syn region logError start=+^\d\{4}-\d\{2}-\d\{2} \d\{2}:\d\{2}:\d\{2}|ERROR|+ end=+^\d\{4}-\d\{2}-\d\{2} \d\{2}:\d\{2}:\d\{2}|+me=e-22
:syn region logException start=+^\d\{4}-\d\{2}-\d\{2} \d\{2}:\d\{2}:\d\{2}|EXCEPTION|+ end=+^\d\{4}-\d\{2}-\d\{2} \d\{2}:\d\{2}:\d\{2}|+me=e-22
:syn region logFatal start=+^\d\{4}-\d\{2}-\d\{2} \d\{2}:\d\{2}:\d\{2}|FATAL|+ end=+^\d\{4}-\d\{2}-\d\{2} \d\{2}:\d\{2}:\d\{2}|+me=e-22
答案 0 :(得分:2)
如果您只有两个小组(DEBUG and TRACE
),则可以
hi def logDebug ctermfg=yellow
hi def logTrace ctermfg=green
syn match logTrace "^\zsTRACE:\_.\{-}\ze\_^DEBUG:"
syn match logDebug "^\zsDEBUG:\_.\{-}\ze\_^TRACE:"
注意我改变了颜色,也改变了时间戳/日期匹配,只是为了显示想法,你可以添加它。
如果您有更多群组......并且所有群组都以时间戳开头。您只需检查时间戳,而不是TRACE/DEBUG/INFO/WARN/...
之后的\ze
。在这里你可以在组中创建密钥,并引用它..
答案 1 :(得分:2)
没有“继续着色直到下一个语法组”的概念;您需要在语法组中包含其他行。
为此,有两种可能性:
:syn match
以包含不以其中一个时间戳/关键字开头的其他行。:syn region
并使区域在日志行的下一个开始处减去一行(对此使用:help :syn-pattern-offset
)或在文件末尾。