-
Notifications
You must be signed in to change notification settings - Fork 0
/
custom_logger.py
79 lines (60 loc) · 2.22 KB
/
custom_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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
from datetime import date
import logging
from logging import handlers
import time
# https://stackoverflow.com/questions/384076
grey = '\033[0;37m'
yellow = '\033[0;33m'
red = '\033[1;31m'
bold_red = '\033[0;31m'
reset = '\033[0m'
format = '%(levelname)8s [%(asctime)s] %(name)21s | %(message)s'
datefmt = '%H:%M:%S'
class ColouredFormatter(logging.Formatter):
FORMATS = {
logging.DEBUG: grey + format + reset,
logging.INFO: format,
logging.WARNING: yellow + format + reset,
logging.ERROR: red + format + reset,
logging.CRITICAL: bold_red + format + reset
}
def format(self, record):
log_fmt = self.FORMATS.get(record.levelno)
formatter = logging.Formatter(log_fmt, self.datefmt)
return formatter.format(record)
class BasicFormatter(logging.Formatter):
FORMATS = {
logging.DEBUG: format,
logging.INFO: format,
logging.WARNING: format,
logging.ERROR: format,
logging.CRITICAL: format
}
def format(self, record):
log_fmt = self.FORMATS.get(record.levelno)
formatter = logging.Formatter(log_fmt, self.datefmt)
return formatter.format(record)
class CustomLogger(logging.Logger):
def __init__(self, name, level=logging.INFO):
super().__init__(name, level)
console_handler = logging.StreamHandler()
console_handler.setFormatter(ColouredFormatter(datefmt='%H:%M:%S'))
today = date.today().isoformat()
file_handler = handlers.TimedRotatingFileHandler(f'gwaff.log',
when='midnight',
backupCount=2)
file_handler.setFormatter(BasicFormatter(datefmt='%H:%M:%S'))
self.addHandler(console_handler)
self.addHandler(file_handler)
# TODO: Fix the logger to not output garbage
logging.setLoggerClass(CustomLogger)
def Logger(name):
return logging.getLogger(name)
if __name__ == '__main__':
logger = Logger("LOGGER")
logger.setLevel(logging.DEBUG)
logger.debug("Debug message")
logger.info("Info message")
logger.warning("Warning message")
logger.error("Error message")
logger.critical("Crticial message")