forked from AY2425S2-CG4002-Team-8/External-comms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.py
More file actions
66 lines (53 loc) · 1.98 KB
/
logger.py
File metadata and controls
66 lines (53 loc) · 1.98 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import logging
from colorlog import ColoredFormatter
# Define custom log levels
AI_P1_LEVEL = 25 # Between WARNING (30) and INFO (20)
AI_P2_LEVEL = 26 # Slightly higher than AI_P1
GE_P1_LEVEL = 27
GE_P2_LEVEL = 28
logging.addLevelName(AI_P1_LEVEL, "AI_P1")
logging.addLevelName(AI_P2_LEVEL, "AI_P2")
logging.addLevelName(GE_P1_LEVEL, "GE_P1")
logging.addLevelName(GE_P2_LEVEL, "GE_P2")
def ai_p1(self, message, *args, **kwargs):
if self.isEnabledFor(AI_P1_LEVEL):
self._log(AI_P1_LEVEL, message, args, **kwargs)
def ai_p2(self, message, *args, **kwargs):
if self.isEnabledFor(AI_P2_LEVEL):
self._log(AI_P2_LEVEL, message, args, **kwargs)
def ge_p1(self, message, *args, **kwargs):
if self.isEnabledFor(GE_P1_LEVEL):
self._log(GE_P1_LEVEL, message, args, **kwargs)
def ge_p2(self, message, *args, **kwargs):
if self.isEnabledFor(GE_P2_LEVEL):
self._log(GE_P2_LEVEL, message, args, **kwargs)
logging.Logger.ai_p1 = ai_p1
logging.Logger.ai_p2 = ai_p2
logging.Logger.ge_p1 = ge_p1
logging.Logger.ge_p2 = ge_p2
def get_logger(name: str) -> logging.Logger:
logger = logging.getLogger(name)
logger.setLevel(logging.DEBUG)
if not logger.handlers:
# Console handler with color
handler = logging.StreamHandler()
handler.setLevel(logging.DEBUG)
# Assign colors to log levels, including our custom level
formatter = ColoredFormatter(
"%(log_color)s%(asctime)s - %(name)s - %(levelname)s - %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
log_colors={
'DEBUG': 'green',
'INFO': 'blue',
'WARNING': 'light_red',
'ERROR': 'red',
'CRITICAL': 'white',
'AI_P1': 'cyan',
'AI_P2': 'purple',
'GE_P1': 'light_cyan',
'GE_P2': 'light_purple'
}
)
handler.setFormatter(formatter)
logger.addHandler(handler)
return logger