Sublime自定义Python日志格式化器语法高亮问题求助
问题描述
我希望基于自定义Python日志格式化器,利用Sublime语法定义实现Python日志输出的语法高亮,但编写的语法定义未生效,无法在Sublime的高亮列表中找到该选项。以下是我的相关代码和示例日志:
我的Sublime语法定义
%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'
自定义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"
示例日志
[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. 修复语法定义中的格式与匹配错误
原语法定义存在YAML转义错误、匹配规则与日志格式不匹配、无效节点等问题,修正后的代码如下:
%YAML 1.2 --- name: TBLog file_extensions: - tblog scope: source.tblog contexts: main: # 匹配带毫秒的时间戳和单字符日志级别 - match: '^\[(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2},\d+)\]\[([A-Z])\]' captures: 1: string.timestamp.tblog 2: string.loglevel.tblog push: log_message log_message: - match: '$' pop: true # 匹配日志中的标签类方括号内容 - match: '\[[^\]]+\]' scope: meta.tags.tblog # 匹配双引号字符串 - 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 # 匹配单引号字符串 - 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 # 匹配关键字 - match: '\b(if|else|for|while)\b' scope: keyword.control.tblog # 匹配整数与小数 - match: '\b(-)?\d+(\.\d+)?\b' scope: constant.numeric.tblog
2. 放置语法文件到正确路径
将修正后的文件保存为TBLog.sublime-syntax,放入Sublime Text的用户包目录:
- Windows:
%APPDATA%\Sublime Text\Packages\User - macOS:
~/Library/Application Support/Sublime Text/Packages/User - Linux:
~/.config/sublime-text/Packages/User
3. 启用语法高亮
重启Sublime Text后,打开.tblog后缀的日志文件,通过快捷键Ctrl+Shift+P(Windows/Linux)或Cmd+Shift+P(macOS)打开命令面板,输入Set Syntax: TBLog即可启用自定义高亮。
内容的提问来源于stack exchange,提问作者Serhat
相关产品推荐
相关产品推荐

