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!
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With