forked from HogaStack/py-node-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.py
More file actions
54 lines (40 loc) · 1.68 KB
/
Copy pathlogger.py
File metadata and controls
54 lines (40 loc) · 1.68 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
import logging
# Custom log formatter to add colors for different log levels and show line numbers
class ColoredFormatter(logging.Formatter):
"""Custom log formatter to add colors for different log levels and show line numbers"""
# ANSI color codes
COLORS = {
'DEBUG': '\033[36m', # cyan
'INFO': '\033[32m', # green
'WARNING': '\033[33m', # yellow
'ERROR': '\033[31m', # red
'CRITICAL': '\033[35m', # purple
'RESET': '\033[0m', # reset
}
def format(self, record: logging.LogRecord) -> str:
# Obtain the color corresponding to the log level
log_color = self.COLORS.get(record.levelname, self.COLORS['RESET'])
reset_color = self.COLORS['RESET']
# Add color to log level
record.levelname = f'{log_color}{record.levelname}{reset_color}'
# Call the format method of the parent class
return super().format(record)
def get_logger(logger: logging.Logger) -> logging.Logger:
"""
Configure and return a custom logger with a specific format and color scheme
Args:
logger (logging.Logger): The logger to configure.
Returns:
logging.Logger: The configured logger.
"""
# Configure the logger
logger.setLevel(logging.INFO)
# Create console processor
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.INFO)
# Create formatter with function name before line number
formatter = ColoredFormatter('%(asctime)s | %(levelname)s | %(name)s:%(funcName)s:%(lineno)d | %(message)s')
console_handler.setFormatter(formatter)
# Add handler to logger
logger.addHandler(console_handler)
return logger