-
Notifications
You must be signed in to change notification settings - Fork 2
/
logger.py
60 lines (45 loc) · 1.99 KB
/
logger.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
51
52
53
54
55
56
57
58
59
60
import logging
import os
import sys
from tkinter.scrolledtext import ScrolledText
import constants
class TextHandler(logging.Handler):
def __init__(self, text: ScrolledText):
# run the regular Handler __init__
logging.Handler.__init__(self)
self.text = text
self.text.tag_config(logging.getLevelName(logging.DEBUG))
self.text.tag_config(logging.getLevelName(logging.INFO))
self.text.tag_config(logging.getLevelName(logging.WARNING), foreground='#fca862')
self.text.tag_config(logging.getLevelName(logging.ERROR), foreground='#ff3333')
self.text.tag_config(logging.getLevelName(logging.CRITICAL), foreground='#b30000')
self.line_limitation = 200.0
self.line_check = 250.0
def num_lines(self):
return int(self.text.index('end').split('.')[0]) - 1
def emit(self, record: logging.LogRecord):
msg = self.format(record)
self.text.insert('end', msg + '\n', record.levelname)
self.text.see('end')
if self.num_lines() > self.line_check:
self.text.delete(1.0, float(min(2, self.line_check - self.line_limitation)))
class Logger:
def __init__(self, custom_handler=None):
"""initialization
"""
log_folder = os.path.join(constants.root_path, 'log')
if not os.path.exists(log_folder):
os.makedirs(log_folder)
file_handler = logging.FileHandler(os.path.join(log_folder, 'forza5.log'), mode='w')
if custom_handler is None:
custom_handler = logging.StreamHandler(sys.stdout)
custom_handler.setLevel(logging.INFO)
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s.%(msecs)03d | \t %(levelname)s:\t %(message)s', datefmt='%Y-%m-%d %H:%M:%S', handlers=[file_handler, custom_handler])
def __call__(self, name: str):
"""get logger
Args:
name (str): logger name
Returns:
[logger]: get logger
"""
return logging.getLogger(name)