How to input variables in logger formatter?

回眸只為那壹抹淺笑 提交于 2020-01-13 09:25:10

问题


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!


回答1:


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



回答2:


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.




回答3:


locals()
FORMAT = '%(MYVAR)s %(asctime)s - %(levelname)s - %(message)s'

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




回答4:


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 and CustomAdapter), such as addHandler(). Admittedly, this is probably less Pythonic but it worked as a quick solution for me.



来源:https://stackoverflow.com/questions/16203908/how-to-input-variables-in-logger-formatter

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!