-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcustom_formatter.py
40 lines (35 loc) · 1.49 KB
/
custom_formatter.py
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
38
39
40
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# author: 'zfb'
# time: 2022-05-03 18:02
import logging
from PyQt5.QtGui import QColor
class CustomFormatter(logging.Formatter):
'''自定义日志的格式化,保证QPlainTextEdit可以用不同颜色显示对应等级的日志
'''
# 定义不同日志等级的格式和颜色
FORMATS = {
# critical专门用于显示其他信息,所以不需要时间日期
logging.CRITICAL: ("[{asctime}]: {message}", "#000000"),
logging.ERROR: ("[{asctime}] {levelname:-<8}--- {message}", QColor("red")),
logging.DEBUG: ("[{asctime}] {levelname:-<8}--- {message}", "green"),
logging.INFO: ("[{asctime}] {levelname:-<8}--- {message}", "#0000FF"),
logging.WARNING: ('[{asctime}] {levelname:-<8}--- {message}', QColor(100, 100, 0))
}
def __init__(self):
'''把格式化设置为{}风格,而不是()%s,从子类修改父类
'''
super().__init__(style="{")
def format(self, record):
'''继承logging.Formatter必须实现的方法
'''
last_fmt = self._style._fmt
opt = CustomFormatter.FORMATS.get(record.levelno)
# 设置时间日期的格式化规则
self.datefmt = "%Y-%m-%d %H:%M:%S"
if opt:
fmt, color = opt
self._style._fmt = "<font color=\"{}\">{}</font>".format(QColor(color).name(),fmt)
res = logging.Formatter.format(self, record)
self._style._fmt = last_fmt
return res