Skip to content
This repository has been archived by the owner on Oct 18, 2024. It is now read-only.

Commit

Permalink
Add multiline indent formatting for log file
Browse files Browse the repository at this point in the history
  • Loading branch information
Pixelguin committed Sep 16, 2023
1 parent bfef7d7 commit 61cf2f2
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion Persona.PrerequisiteInstaller.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit 61cf2f2

Please sign in to comment.