Pass A Counter To Every Python Logging Method
The python logging method can be set to print the time of each event. However if I am running a simulation with its own time variable can that be used in the logger? The logging F
Solution 1:
You could use a logging.Filter
:
import logging
class ContextFilter(logging.Filter):
def filter(self, record):
record.count = counter
return True
logging.basicConfig(
level = logging.DEBUG,
format = '%(levelname)-8s: %(count)s: %(message)s')
logger = logging.getLogger(__name__)
logger.addFilter(ContextFilter())
counter = 5
logger.debug('First Event')
counter += 2
logger.warning('Second Event')
yields
DEBUG : 5: First Event
WARNING : 7: Second Event
Post a Comment for "Pass A Counter To Every Python Logging Method"