Python 日志记录 : Specifying converter attribute of a log formatter in config file

标签 python logging config converter formatter

我希望我的日志文件中的所有时间戳都是 UTC 时间戳。当通过代码指定时,这是按如下方式完成的:

import logging
import time

myHandler = logging.FileHandler('mylogfile.log', 'a')
formatter = logging.Formatter('%(asctime)s %(levelname)-8s %(name)-15s:%(lineno)4s: %(message)-80s')
formatter.converter = time.gmtime

myHandler.setFormatter(formatter)

myLogger = logging.getLogger('MyApp')
myLogger.addHandler(myHandler)

myLogger.setLevel(logging.DEBUG)
myLogger.info('here we are')

我想从上面的“代码内”配置转移到基于配置文件的机制。

这是格式化程序的配置文件部分:

[handler_MyLogHandler]
args=("mylogfile.log", "a",)
class=FileHandler
level=DEBUG
formatter=simpleFormatter

现在,如何在上面的部分中指定转换器属性(time.gmtime)?

编辑:上面的配置文件是这样加载的:

logging.config.fileConfig('myLogConfig.conf')

最佳答案

可悲的是,除了使用配置文件之外,没有办法使用配置文件来做到这一点。一个

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

然后在配置中使用 UTCFormatter

关于Python 日志记录 : Specifying converter attribute of a log formatter in config file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12139648/

相关文章:

java - 如何检查是否加载了 log4j.properties?

java - Logger 和 System.out.println 的输出不按顺序

multithreading - 如何在一台服务器上配置Elasticsearch集群以获得最佳搜索性能

python - 元组的子元组

python - 如何在Python中生成按时间排序的uid?

python - Pandas :查找具有第二高值的行的索引

java - 使用 ConfigurationFactory 以编程方式配置 log4j2

vue.js - 使用Vue CLI3.0创建多页应用程序,如何处理此错误?

bash - 如何在 shell 脚本中获取 INI 值?

python - 在 python 中,如何判断 super 对象正在包装哪个类?