From 9c7c5fb8d2e8f42b0608ee395d771f69636dce64 Mon Sep 17 00:00:00 2001 From: Untone Date: Tue, 12 Mar 2024 17:40:55 +0300 Subject: [PATCH] multiline-logger-fix --- services/logger.py | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/services/logger.py b/services/logger.py index b312999c..69cdc6dc 100644 --- a/services/logger.py +++ b/services/logger.py @@ -33,21 +33,20 @@ fmt_config = { class MultilineColoredFormatter(colorlog.ColoredFormatter): def format(self, record): - # Check if the message is multiline - if record.getMessage() and '\n' in record.getMessage(): - # Split the message into lines - lines = record.getMessage().split('\n') - formatted_lines = [] - for line in lines: - # Format each line with the provided format - formatted_lines.append(super().format(record)) - # Join the formatted lines + message = record.getMessage() + if '\n' in message: + lines = message.split('\n') + record.message = lines[0] # Keep only the first line in the record + formatted_lines = [super().format(record)] # Format the first line + for line in lines[1:]: + record.message = line # Set the message to the remaining lines one by one + formatted_lines.append(self._formatMessage(record)) # Format without prefix return '\n'.join(formatted_lines) else: - # If not multiline or no message, use the default formatting return super().format(record) + # Create a MultilineColoredFormatter object for colorized logging formatter = MultilineColoredFormatter(fmt_string, **fmt_config)