Vim 自动识别 log 文件

警告
本文最后更新于 2021-06-22,文中内容可能已过时。

自动识别 log 文件

1
2
# 在 ~/.vimrc 添加
au BufNewFile,BufRead *.log set filetype=log

添加语法高亮

systax

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
" Vim syntax file
" Language:         Generic log files
" Maintainer:       Alex Dzyoba <avd@reduct.ru>
" Latest Revision:  2013-03-08
" Changes:          2013-03-08 Initial version

" Based on messages.vim - syntax file for highlighting kernel messages

au BufRead,BufNewFile *.log set filetype=log

if exists("b:current_syntax")
  finish
endif

syn match log_fatal     '\c.*\<\(FATAL\|FTL\|FAIL\|FAILED\|FAILURE\).*'
syn match log_error     '\c.*\<\(ERR\|ERROR\|ERRORS\).*'
syn match log_warning   '\c.*\<\(WARNING\|DELETE\|DELETING\|DELETED\|RETRY\|RETRYING\).*'
syn region log_string   start=/'/ end=/'/ end=/$/ skip=/\\./
syn region log_string   start=/"/ end=/"/ skip=/\\./
syn match log_number    '0x[0-9a-fA-F]*\|\[<[0-9a-f]\+>\]\|\<\d[0-9a-fA-F]*'

syn match   log_date '\(Jan\|Feb\|Mar\|Apr\|May\|Jun\|Jul\|Aug\|Sep\|Oct\|Nov\|Dec\) [ 0-9]\d *'
syn match   log_date '\d\{4}-\d\d-\d\d'
syn match   log_date '^202\d\{1}\d\d\d\d'

syn match   log_time '\d\d:\d\d:\d\d\s*'
syn match   log_time '\c\d\d:\d\d:\d\d\(\.\d\+\)\=\([+-]\d\d:\d\d\|Z\)'

hi def link log_string          Comment ""String
hi def link log_number          Comment ""Number
hi def link log_date            Constant
hi def link log_time            Type
hi def link log_fatal           ErrotMsg
hi def link log_error           Error
hi def link log_warning         WarningMsg

let b:current_syntax = "log"

ftdetect

1
2
## 在 ~/.vimrc/ftdetect/log.vim
au BufNewFile,BufRead *.log set filetype=log

相关内容

william 支付宝支付宝
william 微信微信
0%