我的模块中有以下ColoeredFormatter。
但是格式化程序没有被调用。我希望在控制台上“hi”,但我得到“这是根记录器的信息”。
我已经删除了所有.pyc文件,但它没有帮助。
MyModule的/ __初始化__。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")
答案 0 :(得分:12)
我必须将class
替换为()
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:
'()': MyModule.ColoredFormatter
"""
dict = yaml.parse(conf)
logging.config.dictConfig(dict["logging"])
logging.info("This is an info of the root logger")