6
我在我的模块中有以下ColoeredFormatter。python日志记录dictConfig自定义格式化程序不被称为
但格式化程序没有被调用。我希望控制台上有“hi”,但我得到“这是根记录器的信息”。
我已经删除了所有的.pyc文件,但没有帮助。
MyModule的/ __ init__.py
from MyModule.ColoredFormatter import ColoredFormatter
__all__ = ('ColoredFormatter')
MyModule的/ ColoredFormatter.py
import logging
class ColoredFormatter(logging.Formatter):
def __init__(self, default):
self.default = default
def format(self, record):
print("hi")
record.msg = "hi"
return self.default.format(record)
我的脚本
import logging, logging.config, yaml
conf="""
logging:
version: 1
disable_existing_loggers: true
root:
level: !!python/name:logging.NOTSET
handlers: [console]
handlers:
console:
class: logging.StreamHandler
stream: ext://sys.stdout
formatter: myFormatter
level: !!python/name:logging.NOTSET
formatters:
myFormatter:
class: !!python/name:MyModule.ColoredFormatter
"""
dict = yaml.parse(conf)
logging.config.dictConfig(dict["logging"])
logging.info("This is an info of the root logger")
更换
class
你能请详细说明你是如何得到这些信息?我无法在任何地方找到 – Hagai@Hagai一些信息可以在[Python logging documentation](https://docs.python.org/3/library/logging.config.html#dictionary-schema-details)中找到。 – Evert
谢谢@Evert,这有点难以找到,所以对于下一代:你可以在“user-defined-objects”部分找到它https://docs.python.org/2/library/logging.config html的#用户定义的对象 – Hagai