-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlogger.py
More file actions
51 lines (42 loc) · 1.59 KB
/
logger.py
File metadata and controls
51 lines (42 loc) · 1.59 KB
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
'''
Author: tbjuechen
Date: 2024-08-12
Version: 1.0
Description: colored logger
License: MIT
'''
import logging
class ColoredFormatter(logging.Formatter):
# define color for different log levels
COLORS = {
'DEBUG': '\033[90m', # Grey
'INFO': '\033[94m', # Blue
'WARNING': '\033[93m', # Yellow
'ERROR': '\033[91m', # Red
'CRITICAL': '\033[95m' # Magenta
}
RESET = '\033[0m'
MSG_FORMAT = lambda t:f'%(asctime)s {t} %(module)s - %(message)s'
# make sure the log level is in the right color and width
FORMATS = {
logging.DEBUG: MSG_FORMAT(COLORS['DEBUG'] + ' [DEBUG] ' + RESET),
logging.INFO: MSG_FORMAT(COLORS['INFO'] + ' [INFO] ' + RESET),
logging.WARNING: MSG_FORMAT(COLORS['WARNING'] + ' [WARNING] ' + RESET),
logging.ERROR: MSG_FORMAT(COLORS['ERROR'] + ' [ERROR] ' + RESET),
logging.CRITICAL: MSG_FORMAT(COLORS['CRITICAL'] + '[CRITICAL] ' + RESET)
}
def format(self, record):
log_fmt = self.FORMATS.get(record.levelno)
formatter = logging.Formatter(log_fmt, datefmt=self.datefmt)
return formatter.format(record)
# create logger with 'logger'
logger = logging.getLogger('logger')
logger.setLevel(logging.INFO)
# create console handler and set level to debug
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.DEBUG)
# create formatter with datefmt and add it to the handler
formatter = ColoredFormatter(datefmt='%H:%M:%S')
console_handler.setFormatter(formatter)
# add the handler to the logger
logger.addHandler(console_handler)