Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to input variables in logger formatter?

Tags:

python

logging

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!

like image 530
Stephan Tual Avatar asked Apr 24 '13 23:04

Stephan Tual


People also ask

How do you pass variables in logger info?

Logging variable data To log variable data, use a format string for the event description message and append the variable data as arguments. For example: import logging logging.


2 Answers

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
like image 98
unutbu Avatar answered Oct 01 '22 10:10

unutbu


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.

like image 21
Vinay Sajip Avatar answered Oct 01 '22 09:10

Vinay Sajip