From 61cf2f2bf3b992c8db33bff6b45bc59e1aba7b0a Mon Sep 17 00:00:00 2001 From: Pixelguin <11445611+Pixelguin@users.noreply.github.com> Date: Fri, 15 Sep 2023 21:24:31 -0700 Subject: [PATCH] Add multiline indent formatting for log file --- Persona.PrerequisiteInstaller.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/Persona.PrerequisiteInstaller.py b/Persona.PrerequisiteInstaller.py index c4abef3..8637445 100644 --- a/Persona.PrerequisiteInstaller.py +++ b/Persona.PrerequisiteInstaller.py @@ -37,10 +37,26 @@ os.makedirs(INSTALL_LOGS_DIR, exist_ok = True) # Create logger +LOG_FILE_INDENT = 11 + log = logging.getLogger('logger') log.setLevel(logging.DEBUG) -file_formatter = logging.Formatter('>%(levelname)-10s %(message)s') # Show level in log file but not console +class MultilineIndentFormatter(logging.Formatter): + ''' + Custom log formatter for multiline messages. + + This formatter modifies logging.Formatter to add a fixed number of spaces to newline characters. + This ensures multiline log messages in file_formatter are properly indented. + + Returns: + str: The formatted message. + ''' + def format(self, record): + s = super().format(record) + return s.replace('\n', '\n' + ' ' * int(LOG_FILE_INDENT + 1)) + +file_formatter = MultilineIndentFormatter(f'>%(levelname)-{LOG_FILE_INDENT}s%(message)s') # Show level in log file but not console console_formatter = logging.Formatter('%(message)s') file_handler = logging.FileHandler(LOGS_FILE, mode = 'w', encoding = 'utf-8')