core/services/logger.py

42 lines
1.3 KiB
Python
Raw Normal View History

2024-02-20 14:49:21 +00:00
import logging
import colorlog
def get_colorful_logger(name):
2024-02-20 15:16:17 +00:00
# Define the color scheme
color_scheme = {
'DEBUG': 'light_black',
'INFO': 'green',
'WARNING': 'yellow',
'ERROR': 'red',
'CRITICAL': 'red,bg_white',
}
2024-02-20 15:37:53 +00:00
# Define secondary log colors
secondary_colors = {
'log_name': {'DEBUG': 'blue'},
'asctime': {'DEBUG': 'cyan'},
'process': {'DEBUG': 'purple'},
'module': {'DEBUG': 'light_black,bg_blue'},
}
2024-02-20 15:16:17 +00:00
# Create a ColoredFormatter object for colorized logging
2024-02-20 14:49:21 +00:00
formatter = colorlog.ColoredFormatter(
2024-02-20 15:37:53 +00:00
"%(log_color)s%(asctime)s%(asctime)s %(process)s%(process)s %(log_color)s[%(name)s]%(reset)s %(module)s%(module)s %(log_color)s[%(levelname)-8s]%(reset)s %(white)s%(message)s",
2024-02-20 15:16:17 +00:00
log_colors=color_scheme,
2024-02-20 15:37:53 +00:00
secondary_log_colors=secondary_colors, # Set secondary log colors
2024-02-20 15:16:17 +00:00
style='%',
2024-02-20 15:37:53 +00:00
reset=True,
msg_color='white' # Set message color to white
2024-02-20 14:49:21 +00:00
)
2024-02-20 15:16:17 +00:00
# Create a stream handler for logging output
2024-02-20 14:49:21 +00:00
stream = logging.StreamHandler()
stream.setFormatter(formatter)
2024-02-20 15:16:17 +00:00
# Create and configure the logger
2024-02-20 14:49:21 +00:00
logger = logging.getLogger(name)
logger.setLevel(logging.DEBUG)
logger.addHandler(stream)
return logger