How to input variables in logger formatter?

logging, python

Solution

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

Problem

I currently have: ``` FORMAT = '%(asctime)s - %(levelname)s - %(message)s' logging.basicConfig(format=FORMAT, datefmt='%d/%m/%Y %H:%M:%S', filename=LOGFILE, level=getattr(logging, options.loglevel.upper())) ``` ... which works great, however I'm trying to do: ``` FORMAT = '%(MYVAR)s %(asctime)s - %(levelname)s - %(message)s' ``` and that just throws keyerrors, even though `MYVAR` is defined. Is there a workaround? `MYVAR` is a constant, so it would be a shame of having to pass it everytime I invoke the logger. Thank you!

Original source