How to input variables in logger formatter?
You could use a custom filter:
import logging
MYVAR = 'Jabberwocky'
class ContextFilter(logging.Filter):
"""
This is a filter which injects contextual information into the log.
"""
def filter(self, record):
record.MYVAR = MYVAR
return True
FORMAT = '%(MYVAR)s %(asctime)s - %(levelname)s - %(message)s'
logging.basicConfig(format=FORMAT, datefmt='%d/%m/%Y %H:%M:%S')
logger = logging.getLogger(__name__)
logger.addFilter(ContextFilter())
logger.warning("'Twas brillig, and the slithy toves")
yields
Jabberwocky 24/04/2013 20:57:31 - WARNING - 'Twas brillig, and the slithy toves
You could use a custom Filter
, as unutbu
says, or you could use a LoggerAdapter
:
import logging
logger = logging.LoggerAdapter(logging.getLogger(__name__), {'MYVAR': 'Jabberwocky'})
FORMAT = '%(MYVAR)s %(asctime)s - %(levelname)s - %(message)s'
logging.basicConfig(format=FORMAT, datefmt='%d/%m/%Y %H:%M:%S')
logger.warning("'Twas brillig, and the slithy toves")
which gives
Jabberwocky 25/04/2013 07:39:52 - WARNING - 'Twas brillig, and the slithy toves
Alternatively, just pass the information with every call:
import logging
logger = logging.getLogger(__name__)
FORMAT = '%(MYVAR)s %(asctime)s - %(levelname)s - %(message)s'
logging.basicConfig(format=FORMAT, datefmt='%d/%m/%Y %H:%M:%S')
logger.warning("'Twas brillig, and the slithy toves", extra={'MYVAR': 'Jabberwocky'})
which gives the same result.
Since MYVAR is practically constant, the LoggerAdapter
approach requires less code than the Filter
approach in your case.