forked from PauperZ/SSRSpeedN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
50 lines (41 loc) · 1.65 KB
/
__init__.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
41
42
43
44
45
46
47
48
49
50
from sys import stdout
from logging import *
from .color import *
'''
A tricky modification for `logging.StreamHandler`
see `color.py` for some information about
platform support.
'''
# new log level
NOTIFY = INFO + 1
level_to_decos = {
CRITICAL : deco('', FG_RED, bold=True),
ERROR : deco('', FG_RED),
WARNING : deco('', FG_YELLOW, bold=True),
INFO : deco('', FG_WHITE),
DEBUG : deco('', FG_GREEN),
NOTSET : deco('', FG_WHITE), # same as INFO
}
class ConsoleHandler(StreamHandler):
def __init__(self, stream=stdout):
super().__init__(stream)
self.addLogLevel(NOTIFY, 'NOTIFY', deco('', FG_YELLOW))
def addLogLevel(self, level, levelName, deco_str=deco('', FG_WHITE)):
try:
level_to_decos[level] = deco_str
finally:
pass
addLevelName(level, levelName)
def emit(self, record):
const_deco = level_to_decos.get(record.levelno, level_to_decos[NOTSET])
# record.message is dynamically generated by record.getMessage().
record.msg = deco(record.msg, const_deco=const_deco) + reset()
# all properties of `record` that are of string type can be decorated.
record.levelname = deco(record.levelname, bold=True) + reset()
record.filename = deco(record.filename, FG_MAGENTA) + reset()
return super().emit(record)
# def format(self, record):
# ''' NOT COMPATIBLE with `emit()`, don not use them together. '''
# msg = super().format(record)
# const_deco = level_to_decos.get(record.levelno, NOTSET)
# return deco(msg, const_deco=const_deco) + reset()