我知道我可以设置logging.Formatter().converter = time.gmtime.但我怎么能在YAML中做到这一点logging.config.dictConfig?
这是我做的:
version: 1
formatters:
simple:
format: '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
converter: ext://time.gmtime
handlers:
console:
class: logging.StreamHandler
level: DEBUG
formatter: simple
stream: ext://sys.stdout
loggers:
my_app:
level: DEBUG
handlers: [console]
Run Code Online (Sandbox Code Playgroud)
但它仍然记录在当地时间.
该logging.config模块仅支持密钥format,datefmt并且class; 你的converter钥匙完全被忽略了.
该class键也让你指定自定义格式,所以你最好的选择是继承logging.Formatter()和有子类设置转换器:
from logging import Formatter
import time
class GMTFormatter(Formatter):
converter = time.gmtime
Run Code Online (Sandbox Code Playgroud)
然后在你的YAML中引用那个类:
formatters:
simple:
format: '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
class: ext://your.module.GMTFormatter
Run Code Online (Sandbox Code Playgroud)