没有root logger的logging.ini文件可以存在吗? [英] Can I have logging.ini file without root logger?

查看:62
本文介绍了没有root logger的logging.ini文件可以存在吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的logging.ini文件的外观:

Here is how my logging.ini file looks like:

[loggers]
keys=teja

[handlers]
keys=fileHandler

[formatters]
keys=simpleFormatter

[logger_teja]
level=DEBUG
handlers=fileHandler
qualname=tejaLogger

[handler_fileHandler]
class=logging.FileHandler
level=DEBUG
formatter=simpleFormatter
args=("error.log", "w")

[formatter_simpleFormatter]
format=%(asctime)s - %(name)s - %(levelname)s - %(message)s

我收到以下错误:

File "test.py", line 22, in <module>
    logging.config.fileConfig('logging.ini')
  File "/usr/lib/python2.7/logging/config.py", line 79, in fileConfig
    _install_loggers(cp, handlers, disable_existing_loggers)
  File "/usr/lib/python2.7/logging/config.py", line 183, in _install_loggers
    llist.remove("root")
ValueError: list.remove(x): x not in list

请帮助我解决问题.或者请向我解释为什么总是总需要包含root记录器?"

Please help me to figure the problem. Or Please explain me "Why there is always a need to include root logger at all?"

推荐答案

如果您使用源代码,您会看到您必须配置根记录器:

If you use the source, you'll see that you must configure a root logger:

# configure the root first
llist = cp["loggers"]["keys"]
llist = llist.split(",")
llist = list(map(lambda x: x.strip(), llist))
llist.remove("root")
section = cp["logger_root"]
root = logging.root
log = root

(其中 cp configparser 会加载您传入的 .ini 文件)

我能想到的唯一原因是显式要好于隐式,因此如果您认为它会产生一些神奇的效果,它将迫使您准确声明要使用root记录器执行的操作.尽管我不认为这是一个特别好的理由.这可能只是当时某人想到的方式.如果您进行一些进一步阅读:

The only reason I can think of is that explicit is better than implicit, so it forces you to declare exactly what you want to do with the root logger, in case you thought it would do some magic. Though I don't thinkg that's a particularly good reason. It was probably just the way someone thought to do it at the time. If you do some further reading:

fileConfig()API比dictConfig()API古老,并且不提供覆盖日志记录某些方面的功能....将来配置功能的增强功能将添加到dictConfig()中,因此它是值得在方便时考虑过渡到此较新的API.

The fileConfig() API is older than the dictConfig() API and does not provide functionality to cover certain aspects of logging [... N]ote that future enhancements to configuration functionality will be added to dictConfig(), so it’s worth considering transitioning to this newer API when it’s convenient to do so.

如果考虑使用 dictConfig 文档,看来您不必提供 root 记录器.

And if you consider the dictConfig docs, it appears that you don't have to provide a root logger.

因此,看来您需要指定一个根处理程序,除了向后兼容之外,没有其他充分的理由.如果要解决此问题,则必须在Python文件中指定设置或导入JSON文件并使用 dictConfig 方法.

So it appears that you are required to specify a root handler, with no really good reason besides backwards compatibility. If you want to get around that, you'll have to either specify your settings in a Python file or import a JSON file and use the dictConfig method.

这篇关于没有root logger的logging.ini文件可以存在吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆