英文:
Sublime Syntax Highlighting for Custom Python Logging Formatter
问题
I want to highlight my python log outputs with sublime syntax definitions according to my custom python logging formatter. I try below syntax definition but it did not work. I can't see this highlight on my sublime highlight list. Below is my python CustomFormatter class, sublime syntax definitions and example log. Can you give me any solution?
我的目标是使用Sublime的语法定义来突出显示我的Python日志输出,根据我的自定义Python日志格式化程序。我尝试了下面的语法定义,但它没有起作用。我无法在Sublime的高亮列表中看到此突出显示。下面是我的Python CustomFormatter类、Sublime的语法定义和示例日志。你能给我任何解决方案吗?
My syntax definition is:
我的语法定义如下:
%YAML 1.2
---
# See http://www.sublimetext.com/docs/syntax.html
name: TBLog
file_extensions:
- tblog
scope: source.tblog
contexts:
main:
- match: '^\[.*\].*'
captures:
'0': { name: string.timestamp.tblog }
'1': { name: string.loglevel.tblog }
push: log_message
log_message:
- match: '$'
pop: true
- include: string.quoted.double
- include: string.quoted.single
- include: keyword.control
- include: constant.numeric
- include: string.timestamp
string.timestamp:
- match: '\[\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\]'
scope: string.timestamp.tblog
string.quoted.double:
- match: '"'
scope: punctuation.definition.string.begin.tblog
push:
- meta_scope: string.quoted.double.tblog
- match: '\\.'
scope: constant.character.escape.tblog
- match: '"'
scope: punctuation.definition.string.end.tblog
pop: true
string.quoted.single:
- match: '"'
scope: punctuation.definition.string.begin.tblog
push:
- meta_scope: string.quoted.single.tblog
- match: '\\.'
scope: constant.character.escape.tblog
- match: '"'
scope: punctuation.definition.string.end.tblog
pop: true
keyword.control:
- match: '\b(if|else|for|while)\b'
scope: keyword.control.tblog
constant.numeric:
- match: '\b(-)?[0-9.]+\b'
scope: constant.numeric.tblog
string.loglevel:
- match: '\[(WARNING|ERROR|CRITICAL|DEBUG|INFO)\]'
captures:
'0': { name: string.loglevel.tblog }
scope: meta.loglevel.tblog
push: log_message
meta_scope:
string.loglevel.tblog: 'meta.loglevel.tblog'
My custom python formatter is:
我的自定义Python格式化程序如下:
class ColorFormatter(logging.Formatter):
COLORS = {
'WARNING': colorama.Fore.YELLOW + colorama.Style.BRIGHT,
'ERROR': colorama.Fore.RED + colorama.Style.BRIGHT,
'CRITICAL': colorama.Fore.WHITE + colorama.Style.BRIGHT + colorama.Back.RED,
'DEBUG': colorama.Fore.BLUE + colorama.Style.BRIGHT,
'INFO': colorama.Fore.GREEN + colorama.Style.BRIGHT,
'DEFAULT': colorama.Fore.WHITE,
}
def __init__(self, fmt="[%(asctime)s][%(levelname).1s]%(message)s", datefmt="%Y-%m-%d %H:%M:%S"):
logging.Formatter.__init__(self, fmt, datefmt)
def format(self, record):
color = self.COLORS.get(record.levelname, self.COLORS['DEFAULT'])
message = logging.Formatter.format(self, record)
return f"{color}{message}\033[0m"
Example log is:
示例日志如下:
[2023-02-23 22:58:25,226][I]TC] Initializing the bot!
[2023-02-23 22:58:25,241][D]EX] Creating exchange client.
[2023-02-23 22:58:25,678][D]EX] Exchange client has been created!
[2023-02-23 22:58:25,709][I]TC] Preparing bot to start trading!
[2023-02-23 22:58:25,725][D]TC] Variables has been reset!
[2023-02-23 22:58:26,023][D]TC] Server time has been updated! [ST (ms): 1677182307137]
[2023-02-23 22:58:26,038][D]TC] Time difference has been updated! [TD (ms): 1114]
[2023-02-23 22:58:26,337][D]TC] Account order asset balance has been updated! [B: 64.94 BUSD]
[2023-02-23 22:58:26,353][D]TC] Buy order size has been updated! [BOS: 64 BUSD]
[2023-02-23 22:58:26,385][I]TC] Scanning for potential entry opportunities!
英文:
I want to highlight my python log outputs with sublime syntax definitions according to my custom python logging formatter. I try below syntax definition but it did not work. I can't see this highlight on my sublime highlight list. Below is my python CustomFormatter class, sublime syntax definitions and example log. Can you give me any solution?
My syntax definition is:
%YAML 1.2
---
# See http://www.sublimetext.com/docs/syntax.html
name: TBLog
file_extensions:
- tblog
scope: source.tblog
contexts:
main:
- match: '^\[.*\].*'
captures:
'0': { name: string.timestamp.tblog }
'1': { name: string.loglevel.tblog }
push: log_message
log_message:
- match: '$'
pop: true
- include: string.quoted.double
- include: string.quoted.single
- include: keyword.control
- include: constant.numeric
- include: string.timestamp
string.timestamp:
- match: '\[\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\]'
scope: string.timestamp.tblog
string.quoted.double:
- match: '"'
scope: punctuation.definition.string.begin.tblog
push:
- meta_scope: string.quoted.double.tblog
- match: '\\.'
scope: constant.character.escape.tblog
- match: '"'
scope: punctuation.definition.string.end.tblog
pop: true
string.quoted.single:
- match: "'"
scope: punctuation.definition.string.begin.tblog
push:
- meta_scope: string.quoted.single.tblog
- match: '\\.'
scope: constant.character.escape.tblog
- match: "'"
scope: punctuation.definition.string.end.tblog
pop: true
keyword.control:
- match: '\b(if|else|for|while)\b'
scope: keyword.control.tblog
constant.numeric:
- match: '\b(-)?[0-9.]+\b'
scope: constant.numeric.tblog
string.loglevel:
- match: '\[(WARNING|ERROR|CRITICAL|DEBUG|INFO)\]'
captures:
'0': { name: string.loglevel.tblog }
scope: meta.loglevel.tblog
push: log_message
meta_scope:
string.loglevel.tblog: 'meta.loglevel.tblog'
My custom python formatter is:
class ColorFormatter(logging.Formatter):
COLORS = {
'WARNING': colorama.Fore.YELLOW + colorama.Style.BRIGHT,
'ERROR': colorama.Fore.RED + colorama.Style.BRIGHT,
'CRITICAL': colorama.Fore.WHITE + colorama.Style.BRIGHT + colorama.Back.RED,
'DEBUG': colorama.Fore.BLUE + colorama.Style.BRIGHT,
'INFO': colorama.Fore.GREEN + colorama.Style.BRIGHT,
'DEFAULT': colorama.Fore.WHITE,
}
def __init__(self, fmt="[%(asctime)s][%(levelname).1s]%(message)s", datefmt="%Y-%m-%d %H:%M:%S"):
logging.Formatter.__init__(self, fmt, datefmt)
def format(self, record):
color = self.COLORS.get(record.levelname, self.COLORS['DEFAULT'])
message = logging.Formatter.format(self, record)
return f"{color}{message}\033[0m"
Example log is:
[2023-02-23 22:58:25,226][I]TC] Initializing the bot!
[2023-02-23 22:58:25,241][D]EX] Creating exchange client.
[2023-02-23 22:58:25,678][D]EX] Exchange client has been created!
[2023-02-23 22:58:25,709][I]TC] Preparing bot to start trading!
[2023-02-23 22:58:25,725][D]TC] Variables has been reset!
[2023-02-23 22:58:26,023][D]TC] Server time has been updated! [ST (ms): 1677182307137]
[2023-02-23 22:58:26,038][D]TC] Time difference has been updated! [TD (ms): 1114]
[2023-02-23 22:58:26,337][D]TC] Account order asset balance has been updated! [B: 64.94 BUSD]
[2023-02-23 22:58:26,353][D]TC] Buy order size has been updated! [BOS: 64 BUSD]
[2023-02-23 22:58:26,385][I]TC] Scanning for potential entry opportunities!
答案1
得分: 1
不要回答我要翻译的问题。以下是要翻译的内容:
如果你打开Sublime Text控制台(视图菜单 -> 显示控制台),当你保存你的sublime-syntax
文件时,你会看到一个错误消息:
> error parsing lexer: Packages/User/tblog.sublime-syntax: patterns must be a vector at line 67 column 5
第67行是在你的meta_scope
上下文下面的那一行。
移除那个上下文,因为它没有被任何地方引用,会得到以下错误:
> error parsing lexer: Packages/User/tblog.sublime-syntax: capture scope must be a string at line 10 column 7
如果你查看文档中的示例捕获,你会看到它说明了你不应该在捕获组数字上使用字符串引号,它期望一个普通字符串而不是一个包含name:
的映射。
修复了所有这些问题之后,它仍然不会像你期望的那样高亮显示,因为在main
上下文中你在第一个匹配模式的末尾有一个.*
。移除它会得到以下内容:
%YAML 1.2
---
# 详见http://www.sublimetext.com/docs/syntax.html
name: TBLog
file_extensions:
- tblog
scope: source.tblog
contexts:
main:
- match: '^\[.*\]'
captures:
0: string.timestamp.tblog
1: string.loglevel.tblog
push: log_message
log_message:
- match: '$'
pop: true
- include: string.quoted.double
- include: string.quoted.single
- include: keyword.control
- include: constant.numeric
- include: string.timestamp
string.timestamp:
- match: '\[\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\]'
scope: string.timestamp.tblog
string.quoted.double:
- match: '"'
scope: punctuation.definition.string.begin.tblog
push:
- meta_scope: string.quoted.double.tblog
- match: '\\.'
scope: constant.character.escape.tblog
- match: '"'
scope: punctuation.definition.string.end.tblog
pop: true
string.quoted.single:
- match: "'"
scope: punctuation.definition.string.begin.tblog
push:
- meta_scope: string.quoted.single.tblog
- match: '\\.'
scope: constant.character.escape.tblog
- match: "'"
scope: punctuation.definition.string.end.tblog
pop: true
keyword.control:
- match: '\b(if|else|for|while)\b'
scope: keyword.control.tblog
constant.numeric:
- match: '\b(-)?[0-9.]+\b'
scope: constant.numeric.tblog
string.loglevel:
- match: '\[(WARNING|ERROR|CRITICAL|DEBUG|INFO)\]'
captures:
'0': string.loglevel.tblog
scope: meta.loglevel.tblog
push: log_message
然后在启用了TBLog语法的情况下,你的文本在Sublime Text中会看起来像这样:
英文:
If you open the Sublime Text console (View menu -> Show Console), you will see an error when you save your sublime-syntax
file:
> error parsing lexer: Packages/User/tblog.sublime-syntax: patterns must be a vector at line 67 column 5
Line 67 is the line under your meta_scope
context.
Removing that context, as it isn't referenced anywhere, gives us:
> error parsing lexer: Packages/User/tblog.sublime-syntax: capture scope must be a string at line 10 column 7
If you look at the example captures
in the documentation, you'll see how you shouldn't string quote the capture group numbers and it expects a plain string instead of a map with a name:
.
After fixing all that, it still won't highlight the way you might expect because of the .*
you have at the end of your first match pattern in the main
context. Removing that gives:
%YAML 1.2
---
# See http://www.sublimetext.com/docs/syntax.html
name: TBLog
file_extensions:
- tblog
scope: source.tblog
contexts:
main:
- match: '^\[.*\]'
captures:
0: string.timestamp.tblog
1: string.loglevel.tblog
push: log_message
log_message:
- match: '$'
pop: true
- include: string.quoted.double
- include: string.quoted.single
- include: keyword.control
- include: constant.numeric
- include: string.timestamp
string.timestamp:
- match: '\[\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\]'
scope: string.timestamp.tblog
string.quoted.double:
- match: '"'
scope: punctuation.definition.string.begin.tblog
push:
- meta_scope: string.quoted.double.tblog
- match: '\\.'
scope: constant.character.escape.tblog
- match: '"'
scope: punctuation.definition.string.end.tblog
pop: true
string.quoted.single:
- match: "'"
scope: punctuation.definition.string.begin.tblog
push:
- meta_scope: string.quoted.single.tblog
- match: '\\.'
scope: constant.character.escape.tblog
- match: "'"
scope: punctuation.definition.string.end.tblog
pop: true
keyword.control:
- match: '\b(if|else|for|while)\b'
scope: keyword.control.tblog
constant.numeric:
- match: '\b(-)?[0-9.]+\b'
scope: constant.numeric.tblog
string.loglevel:
- match: '\[(WARNING|ERROR|CRITICAL|DEBUG|INFO)\]'
captures:
'0': string.loglevel.tblog
scope: meta.loglevel.tblog
push: log_message
and then your text looks like this in ST with your TBLog syntax enabled:
答案2
得分: 0
I used below YAML syntax code.
And below tmTheme.xml for colors.
And below is how its look like.
https://i.stack.imgur.com/T8iL7.jpg
英文:
I used below YAML syntax code.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
%YAML 1.2
---
# See http://www.sublimetext.com/docs/syntax.html
name: TBLog
file_extensions:
- tblog
scope: tb
contexts:
log_line:
# data
- match: '\[([^\[\]]+)\]'
scope: tb.data
push: log_line
- match: '$'
pop: true
main:
# debug
- match: '\[(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})\]\[D\]'
scope: tb.debug
push: log_line
# info
- match: '\[(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})\]\[I\]'
scope: tb.info
push: log_line
# warning
- match: '\[(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})\]\[W\]'
scope: tb.warning
push: log_line
# error
- match: '\[(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})\]\[E\]'
scope: tb.error
push: log_line
# critical
- match: '\[(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})\]\[C\]'
scope: tb.critical
push: log_line
# datetime
- match: '\[(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})\]'
scope: tb.datetime
push:
- match: '\[D\]'
scope: tb.debug
pop: true
- match: '\[I\]'
scope: tb.info
pop: true
- match: '\[W\]'
scope: tb.warning
pop: true
- match: '\[E\]'
scope: tb.error
pop: true
- match: '\[C\]'
scope: tb.critical
pop: true
<!-- end snippet -->
And below tmTheme.xml for colors.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>name</key>
<string>TBLog</string>
<key>settings</key>
<array>
<dict>
<key>settings</key>
<dict>
<key>background</key>
<string>#272822</string>
<key>caret</key>
<string>#F8F8F0</string>
<key>foreground</key>
<string>#F8F8F2</string>
<key>invisibles</key>
<string>#5E5E5E</string>
<key>line_highlight</key>
<string>#3E3D32</string>
<key>selection</key>
<string>#49483E</string>
<key>findHighlightForeground</key>
<string>#000000</string>
<key>selectionBorder</key>
<string>#222218</string>
<key>activeGuide</key>
<string>#9D550FB0</string>
<key>bracketsForeground</key>
<string>#F8F8F2A5</string>
<key>bracketsOptions</key>
<string>underline</string>
<key>bracketsContentsForeground</key>
<string>#F8F8F2A5</string>
<key>bracketsContentsOptions</key>
<string>underline</string>
<key>tagsOptions</key>
<string>stippled_underline</string>
</dict>
</dict>
<dict>
<key>name</key>
<string>Datetime</string>
<key>scope</key>
<string>tb.datetime</string>
<key>settings</key>
<dict>
<key>foreground</key>
<string>#00B764</string>
</dict>
</dict>
<dict>
<key>name</key>
<string>Data</string>
<key>scope</key>
<string>tb.data</string>
<key>settings</key>
<dict>
<key>foreground</key>
<string>#FFA500</string>
</dict>
</dict>
<dict>
<key>name</key>
<string>Debug</string>
<key>scope</key>
<string>tb.debug</string>
<key>settings</key>
<dict>
<key>foreground</key>
<string>#16ACBA</string>
</dict>
</dict>
<dict>
<key>name</key>
<string>Info</string>
<key>scope</key>
<string>tb.info</string>
<key>settings</key>
<dict>
<key>foreground</key>
<string>#00B764</string>
</dict>
</dict>
<dict>
<key>name</key>
<string>Warning</string>
<key>scope</key>
<string>tb.warning</string>
<key>settings</key>
<dict>
<key>foreground</key>
<string>#EDD436</string>
</dict>
</dict>
<dict>
<key>name</key>
<string>Error</string>
<key>scope</key>
<string>tb.error</string>
<key>settings</key>
<dict>
<key>background</key>
<string>#FF0000</string>
<key>foreground</key>
<string>#FFFFFF</string>
</dict>
</dict>
<dict>
<key>name</key>
<string>Critical</string>
<key>scope</key>
<string>tb.critical</string>
<key>settings</key>
<dict>
<key>background</key>
<string>#FF0000</string>
<key>foreground</key>
<string>#FFFFFF</string>
</dict>
</dict>
</array>
</dict>
</plist>
<!-- end snippet -->
And below is how its look like.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论