multiline-logger-fix

This commit is contained in:
Untone 2024-03-12 17:40:55 +03:00
parent e99acd591a
commit 9c7c5fb8d2

View File

@ -33,21 +33,20 @@ fmt_config = {
class MultilineColoredFormatter(colorlog.ColoredFormatter): class MultilineColoredFormatter(colorlog.ColoredFormatter):
def format(self, record): def format(self, record):
# Check if the message is multiline message = record.getMessage()
if record.getMessage() and '\n' in record.getMessage(): if '\n' in message:
# Split the message into lines lines = message.split('\n')
lines = record.getMessage().split('\n') record.message = lines[0] # Keep only the first line in the record
formatted_lines = [] formatted_lines = [super().format(record)] # Format the first line
for line in lines: for line in lines[1:]:
# Format each line with the provided format record.message = line # Set the message to the remaining lines one by one
formatted_lines.append(super().format(record)) formatted_lines.append(self._formatMessage(record)) # Format without prefix
# Join the formatted lines
return '\n'.join(formatted_lines) return '\n'.join(formatted_lines)
else: else:
# If not multiline or no message, use the default formatting
return super().format(record) return super().format(record)
# Create a MultilineColoredFormatter object for colorized logging # Create a MultilineColoredFormatter object for colorized logging
formatter = MultilineColoredFormatter(fmt_string, **fmt_config) formatter = MultilineColoredFormatter(fmt_string, **fmt_config)