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!
Borrowing from a comment above, I found that the simplest way to do this when the variable is static for all log entries is to simply include it in the formatter itself:
FORMAT = '{} %(asctime)s - %(levelname)s - %(message)s'.format(MYVAR)
With this method, no custom class implementation is required, and you don't have to worry about methods which are not defined for the various classes (
LoggerAdapter
andCustomAdapter
), such asaddHandler()
. Admittedly, this is probably less Pythonic but it worked as a quick solution for me.locals() should return a dictionary of all variables locally available, then the error. If you don't see it in there, then its not locally available. This will prove its not defined properly. We would need more code to see if it was defined improperly. Alternatively you can try "globals()" to check the global ones.... but you probably arent putting "global MYVAR " in the definition that outputs FORMAT
You could use a custom
Filter
, asunutbu
says, or you could use aLoggerAdapter
:which gives
Jabberwocky 25/04/2013 07:39:52 - WARNING - 'Twas brillig, and the slithy toves
Alternatively, just pass the information with every call:
which gives the same result.
Since MYVAR is practically constant, the
LoggerAdapter
approach requires less code than theFilter
approach in your case.You could use a custom filter:
yields