I'm working on converting to the logging
library for all my logging, but I've been stuck on this step for a while. I typically log to the console with some color code escape sequences for formatting. This works well in the console, but not in the log files, which shouldn't have any escape sequences. Therefore, I have a function that trims the escape sequences. However, I can't figure out how to link that function to the formatter that I use for console outputs.
Here's what I have for logging setup:
file_formatter = new_logging.Formatter(log_file_format)
file_handler = logging.FileHandler(output_path)
file_handler.setLevel(logging.DEBUG)
file_handler.setFormatter(file_formatter)
logging.getLogger().addHandler(file_handler)
console_formatter = logging.Formatter(log_console_format)
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.INFO)
console_handler.setFormatter(console_formatter)
logging.getLogger().addHandler(console_handler)
Essentially, my goal is to automatically run a function remove_escape_sequences(log_string)
as part of the file_formatter
. Right now it's just set to be a string with placeholders, '%(asctime)s %(levelname)8s: "%(name)24s" - %(message)s'
. How can I do this?