Source From Here
Question
What is the simplest method for temporarily changing the logging message format, in Python (through the logging module)?
How-To
Here is a simple solution, that can be deduced from Vinay Sajip's own HOWTO; it basically updates the logging formatter with setFormatter():
This correctly produces:
Supplement
* FAQ - How to get the list all existing loggers using python.logging module
Question
What is the simplest method for temporarily changing the logging message format, in Python (through the logging module)?
How-To
Here is a simple solution, that can be deduced from Vinay Sajip's own HOWTO; it basically updates the logging formatter with setFormatter():
- import logging
- logger = logging.getLogger() # Logger
- logger_handler = logging.StreamHandler() # Handler for the logger
- logger.addHandler(logger_handler)
- # First, generic formatter:
- logger_handler.setFormatter(logging.Formatter('%(message)s'))
- logger.error('error message') # Test
- # New formatter for the handler:
- logger_handler.setFormatter(logging.Formatter('PROCESSING FILE xxx - %(message)s'))
- logger.error('error message') # Test
Supplement
* FAQ - How to get the list all existing loggers using python.logging module
- import logging
- loggers = [logging.getLogger(name) for name in logging.root.manager.loggerDict]
沒有留言:
張貼留言