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
This correctly produces:
error message
PROCESSING FILE xxx - error message
(where xxx
can be set dynamically to the file being processed, as asked for in the question).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…