2024-02-21 11:23:42 +00:00
|
|
|
import logging
|
|
|
|
import colorlog
|
|
|
|
|
|
|
|
# Define the color scheme
|
|
|
|
color_scheme = {
|
2024-02-21 16:14:58 +00:00
|
|
|
'DEBUG': 'light_black',
|
|
|
|
'INFO': 'green',
|
|
|
|
'WARNING': 'yellow',
|
|
|
|
'ERROR': 'red',
|
|
|
|
'CRITICAL': 'red,bg_white',
|
2024-02-21 11:23:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# Define secondary log colors
|
|
|
|
secondary_colors = {
|
2024-02-21 16:14:58 +00:00
|
|
|
'log_name': {'DEBUG': 'blue'},
|
|
|
|
'asctime': {'DEBUG': 'cyan'},
|
|
|
|
'process': {'DEBUG': 'purple'},
|
|
|
|
'module': {'DEBUG': 'light_black,bg_blue'},
|
2024-02-27 13:33:25 +00:00
|
|
|
'funcName': {'DEBUG': 'light_white,bg_blue'}, # Add this line
|
2024-02-21 11:23:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# Define the log format string
|
2024-02-27 13:33:25 +00:00
|
|
|
fmt_string = '%(log_color)s%(levelname)s: %(log_color)s[%(module)s.%(funcName)s]%(reset)s %(white)s%(message)s'
|
2024-02-21 11:23:42 +00:00
|
|
|
|
|
|
|
# Define formatting configuration
|
|
|
|
fmt_config = {
|
2024-02-21 16:14:58 +00:00
|
|
|
'log_colors': color_scheme,
|
|
|
|
'secondary_log_colors': secondary_colors,
|
|
|
|
'style': '%',
|
|
|
|
'reset': True,
|
2024-02-21 11:23:42 +00:00
|
|
|
}
|
|
|
|
|
2024-03-03 13:59:15 +00:00
|
|
|
|
2024-02-21 11:23:42 +00:00
|
|
|
class MultilineColoredFormatter(colorlog.ColoredFormatter):
|
2024-03-12 15:14:34 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.log_colors = kwargs.pop('log_colors', {})
|
|
|
|
self.secondary_log_colors = kwargs.pop('secondary_log_colors', {})
|
|
|
|
|
2024-02-21 11:23:42 +00:00
|
|
|
def format(self, record):
|
2024-03-12 14:40:55 +00:00
|
|
|
message = record.getMessage()
|
|
|
|
if '\n' in message:
|
|
|
|
lines = message.split('\n')
|
|
|
|
record.message = lines[0] # Keep only the first line in the record
|
2024-03-12 15:14:34 +00:00
|
|
|
formatted_lines = [super().format(record)] # Format the first line with prefix
|
2024-03-12 14:40:55 +00:00
|
|
|
for line in lines[1:]:
|
|
|
|
record.message = line # Set the message to the remaining lines one by one
|
2024-03-12 15:14:34 +00:00
|
|
|
formatted_lines.append(self.format_secondary_line(record)) # Format without prefix
|
2024-02-21 16:14:58 +00:00
|
|
|
return '\n'.join(formatted_lines)
|
2024-02-21 11:23:42 +00:00
|
|
|
else:
|
|
|
|
return super().format(record)
|
|
|
|
|
2024-03-12 15:14:34 +00:00
|
|
|
def format_secondary_line(self, record):
|
2024-03-12 15:17:28 +00:00
|
|
|
msg = record.getMessage()
|
2024-03-12 15:14:34 +00:00
|
|
|
log_color = self.log_colors.get(record.levelname, 'reset')
|
|
|
|
formatted_msg = []
|
|
|
|
|
2024-03-12 15:17:28 +00:00
|
|
|
# Since there are no secondary log colors in the subsequent lines,
|
|
|
|
# we apply the log_color to each part of the message.
|
2024-03-12 15:14:34 +00:00
|
|
|
for part in msg.split(' '):
|
2024-03-12 15:17:28 +00:00
|
|
|
formatted_msg.append(f"{log_color}{part}{self.reset}")
|
2024-03-12 15:14:34 +00:00
|
|
|
|
|
|
|
return ' '.join(formatted_msg)
|
|
|
|
|
2024-03-06 09:25:55 +00:00
|
|
|
|
2024-03-12 14:40:55 +00:00
|
|
|
|
2024-03-12 15:17:28 +00:00
|
|
|
|
2024-02-21 11:23:42 +00:00
|
|
|
# Create a MultilineColoredFormatter object for colorized logging
|
|
|
|
formatter = MultilineColoredFormatter(fmt_string, **fmt_config)
|
|
|
|
|
|
|
|
# Create a stream handler for logging output
|
|
|
|
stream = logging.StreamHandler()
|
|
|
|
stream.setFormatter(formatter)
|
|
|
|
|
2024-03-06 09:25:55 +00:00
|
|
|
|
2024-02-21 16:14:58 +00:00
|
|
|
def get_colorful_logger(name='main'):
|
2024-02-21 11:23:42 +00:00
|
|
|
# Create and configure the logger
|
|
|
|
logger = logging.getLogger(name)
|
|
|
|
logger.setLevel(logging.DEBUG)
|
|
|
|
logger.addHandler(stream)
|
|
|
|
|
|
|
|
return logger
|
|
|
|
|
2024-03-06 09:25:55 +00:00
|
|
|
|
2024-02-21 11:23:42 +00:00
|
|
|
# Set up the root logger with the same formatting
|
|
|
|
root_logger = logging.getLogger()
|
|
|
|
root_logger.setLevel(logging.DEBUG)
|
|
|
|
root_logger.addHandler(stream)
|
2024-03-11 08:16:12 +00:00
|
|
|
|
|
|
|
ignore_logs = ['_trace', 'httpx', '_client', '_trace.atrace', 'aiohttp', '_client']
|
|
|
|
for lgr in ignore_logs:
|
|
|
|
loggr = logging.getLogger(lgr)
|
|
|
|
loggr.setLevel(logging.INFO)
|