如何在Python模块中正确添加自定义日志记录筛选器

时间:2016-11-28 14:29:44

标签: python python-2.7 logging

我在macOS Sierra下运行Python 2.7.12中的以下片段,但我得到KeyError s:

import logging
from PIL import Image


class TaskAddingFilter(logging.Filter):
    def __init__(self):
        logging.Filter.__init__(self)

    def filter(self, record):
        record.args = record.args + ('task', '')


logging.basicConfig(
    filename='mylog.txt',
    format='%(asctime)-19.19s|%(task)-36s|%(levelname)s:%(name)s:%(lineno)s: %(message)s',
    level=eval('logging.%s' % 'DEBUG'))

# My attempt to "monkey-patch" PIL's logger 
for name, logger in logging.Logger.manager.loggerDict.iteritems():
    logger = logging.getLogger(name)
    if name.startswith('PIL'):
        logger.addFilter(TaskAddingFilter())

logger = logging.getLogger('demo')


def tryThis():
    with open('my_image.png', 'rb') as im:
        logger.debug('Attempting to read image size...', extra={'task': '123'})
        try:
            image = Image.open(im)
            w, h = image.size
            image.save('my_image_out.png', 'PNG')
        except IOError:
            logger.error('Processing failed!', extra={'task': '123'})
            raise Exception()


tryThis()

我得到的错误是:

Traceback (most recent call last):
  File "/usr/local/Cellar/python/2.7.12_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/__init__.py", line 861, in emit
    msg = self.format(record)
  File "/usr/local/Cellar/python/2.7.12_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/__init__.py", line 734, in format
    return fmt.format(record)
  File "/usr/local/Cellar/python/2.7.12_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/__init__.py", line 469, in format
    s = self._fmt % record.__dict__
KeyError: 'task'
Logged from file PngImagePlugin.py, line 135

有什么想法吗?

提前致谢。

2 个答案:

答案 0 :(得分:1)

更好的解决方案是明确添加所需的处理程序(而不是使用basicConfig())并将过滤器附加到它们。然后,不需要修补PIL(或任何其他)记录器,也不需要将extra传递给记录调用。过滤方法只需要设置

record.task = 'foo'

而不是弄乱record.args

答案 1 :(得分:0)

所以对于其他感兴趣的人,我最后开了一个issue on Pillow's repository并感谢贡献者@wiredfool我提出了以下解决方案:

import logging
from PIL import Image


class TaskAddingFilter(logging.Filter):
    def __init__(self):
        logging.Filter.__init__(self)

    def filter(self, record):
        record.args = record.args + ('task', '123')


logger = logging.getLogger('THEDEMO')

Image.init()

logging.basicConfig(
    filename='mylog.txt',
    format='%(asctime)-19.19s|%(task)-36s|%(levelname)s:%(name)s:%(lineno)s: %(message)s',
    level=eval('logging.%s' % 'DEBUG'))

for name, elogger in logging.Logger.manager.loggerDict.iteritems():
    elogger = logging.getLogger(name)
    if name.startswith('PIL'):
        elogger.addFilter(TaskAddingFilter())


def tryThis():
    with open('my_image.png', 'rb') as im:
        logger.debug('Attempting to read image size...', extra={'task': '123'})
        try:
            image = Image.open(im)
            w, h = image.size
            image.save('my_image_out.png', 'PNG')
            print 'all ran well'
        except IOError:
            logger.error('Processing failed!', extra={'task': 123})
            raise Exception()


tryThis()