本文介绍了使用dictConfig时如何重新配置​​记录器格式化程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 dictConfig 设置日志记录.我的要求之一是将格式化程序的默认转换器(我为所有处理程序使用单个格式化程序simpleFormatter)更改为time.gmtime.可以像这样完成:

formatter.converter = time.gmtime

如果我有权使用格式化程序.但是我没有,所以我不能更改默认转换器.我可以想到两种方法来做自己想要的事情:

  • formatters部分中通过dictConfig传递相关参数(类似于'converter': 'ext://time.gmtime'),但是我认为dictConfig
  • 不支持此额外参数
  • 在执行dictConfig之后获取格式化程序,并手动应用配置:formatter.converter = time.gmtime.我不知道如何通过名称获取格式化程序,也不知道它是否受支持,或者是否会在logging.config模块周围乱砍.

在查看日志记录模块的源代码之后,我既未找到示例,也未找到文档,也未找到实现此目的的方法.

有人使用dictConfig设置成功设置了formatter.converter吗?

解决方案

以下是操作方法示例:

import logging
import logging.config
import time

class UTCFormatter(logging.Formatter):
    converter = time.gmtime

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'utc': {
            '()': UTCFormatter,
            'format': '%(asctime)s %(message)s',
        },
        'local': {
            'format': '%(asctime)s %(message)s',
        }
    },
    'handlers': {
        'console1': {
            'class': 'logging.StreamHandler',
            'formatter': 'utc',
        },
        'console2': {
            'class': 'logging.StreamHandler',
            'formatter': 'local',
        },
    },
    'root': {
        'handlers': ['console1', 'console2'],
   }
}

if __name__ == '__main__':
    logging.config.dictConfig(LOGGING)
    logging.warning('Look out!')

运行上面的命令时,我得到:

2015-10-17 12:20:36,217 Look out!
2015-10-17 13:20:36,217 Look out!

使用'()'键进行自定义实例记录在All other keys ...开头的段落中.

I am using dictConfig to setup logging. One of the requirements I have is to change the default converter of the formatter (I am using a single formatter simpleFormatter for all my handlers) to time.gmtime. This would be done like this:

formatter.converter = time.gmtime

If I had access to the formatter. But I don't, so I can not change the default converter. I can think of two ways of doing what I want:

  • pass the relevant parameter via dictConfig, in the formatters section (something like 'converter': 'ext://time.gmtime') But I think this extra parameter is not supported by dictConfig
  • get the formatter after doing a dictConfig and apply the configuration manually: formatter.converter = time.gmtime. I do not know how to get the formatter by name, or whether it is supported or if would be hacking around the logging.config module.

I have found neither examples, nor documentation, nor a way to implement this after taking a look at the source code of the logging module.

Has somebody managed to setup the formatter.converter using a dictConfig setup?

解决方案

Here's an example of how to do it:

import logging
import logging.config
import time

class UTCFormatter(logging.Formatter):
    converter = time.gmtime

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'utc': {
            '()': UTCFormatter,
            'format': '%(asctime)s %(message)s',
        },
        'local': {
            'format': '%(asctime)s %(message)s',
        }
    },
    'handlers': {
        'console1': {
            'class': 'logging.StreamHandler',
            'formatter': 'utc',
        },
        'console2': {
            'class': 'logging.StreamHandler',
            'formatter': 'local',
        },
    },
    'root': {
        'handlers': ['console1', 'console2'],
   }
}

if __name__ == '__main__':
    logging.config.dictConfig(LOGGING)
    logging.warning('Look out!')

When I run the above, I get:

2015-10-17 12:20:36,217 Look out!
2015-10-17 13:20:36,217 Look out!

The use of the '()' key for custom instantiations is documented here in the paragraph beginning All other keys ....

这篇关于使用dictConfig时如何重新配置​​记录器格式化程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-18 20:12