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!