-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.py
82 lines (71 loc) · 2.35 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import logging
import os
import sys
colors = {
'black': "\x1b[30m",
'red': "\x1b[31m",
'green': "\x1b[32m",
'yellow': "\x1b[33m",
'blue': "\x1b[34m",
'magenta': "\x1b[35m",
'cyan': "\x1b[36m",
'white': "\x1b[37m",
'bright_black': "\x1b[90m",
'bright_red': "\x1b[91m",
'bright_green': "\x1b[92m",
'bright_yellow': "\x1b[93m",
'bright_blue': "\x1b[94m",
'bright_magenta': "\x1b[95m",
'bright_cyan': "\x1b[96m",
'bright_white': "\x1b[97m",
'reset': "\x1b[0m"
}
level_colors = {
'DEBUG': colors['bright_blue'],
'INFO': colors['bright_white'],
'WARNING': colors['bright_yellow'],
'ERROR': colors['bright_red'],
'CRITICAL': colors['magenta']
}
message_colors = {
'DEBUG': colors['bright_cyan'],
'INFO': colors['green'],
'WARNING': colors['yellow'],
'ERROR': colors['red'],
'CRITICAL': colors['bright_magenta']
}
class CustomFormatter(logging.Formatter):
def format(self, record):
level_color = level_colors.get(record.levelname, colors['white'])
message_color = message_colors.get(record.levelname, colors['white'])
reset_color = colors['reset']
log_format = (
f"{colors['blue']}%(pathname)s{reset_color}:%(lineno)d{reset_color}: "
f"{colors['red']}%(asctime)s{reset_color} "
f"{colors['cyan']}%(name)s{reset_color} "
f"{level_color}[%(levelname)s]{reset_color} "
f"{message_color}%(message)s{reset_color}"
)
formatter = logging.Formatter(log_format, datefmt="%Y-%m-%d %H:%M:%S")
return formatter.format(record)
stdout_handler = logging.StreamHandler(stream=sys.stdout)
stdout_handler.setFormatter(CustomFormatter())
handlers = [stdout_handler]
log_filename = os.getenv("LOG_FILENAME", f"{__file__}.log")
try:
file_handler = logging.FileHandler(filename=log_filename)
file_handler.setFormatter(CustomFormatter())
handlers.append(file_handler)
except (OSError, IOError) as e:
print(f"Failed to create file handler: {e}", file=sys.stderr)
logging.basicConfig(
level=os.getenv("CE_LOG_LEVEL", "INFO").upper(),
handlers=handlers,
force=True
)
logger = logging.getLogger(__name__)
# logger.debug("Debug message")
# logger.info("Info message")
# logger.warning("Warning message")
# logger.error("Error message")
# logger.critical("Critical message")