python - 我可以在没有根记录器的情况下拥有logging.ini 文件吗?

标签 python logging configuration ini

这是我的logging.ini 文件的样子:

[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

请帮我解决这个问题。 或者 请解释一下“为什么总是需要包含根记录器?”

最佳答案

如果您use the source ,您会发现您必须配置根记录器:

# 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 是加载您传入的 .ini 文件的 configparser)

我能想到的唯一原因是explicit is better than implicit ,因此它迫使您准确声明您想要对根记录器执行的操作,以防您认为它会发挥一些作用。虽然我不认为这是一个特别好的理由。这可能只是当时某人的想法。如果你做一些further reading :

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 docs ,看来您不必提供 root 记录器。

所以看来您需要指定一个根处理程序,除了向后兼容性之外没有真正好的理由。如果您想解决这个问题,则必须在 Python 文件中指定设置,或者导入 JSON 文件并使用 dictConfig 方法。

关于python - 我可以在没有根记录器的情况下拥有logging.ini 文件吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28737858/

相关文章:

C# SOAP Web 服务客户端 - 显示如何记录所有原始 SOAP 输入和输出的示例?

java - 如何使用 java.util.logging.* 更改存储日志的目录

scala - Akka 在 Actor 之外进行日志记录

configuration - 使用 web.config 在 Autofac 中注册工厂类

wcf - 通过 HTTPS 上的客户端证书对 WCF 请求进行身份验证

Pythonic/djangonic 以秒为单位处理用户超时的方式(如果需要的话,也可以以分钟为单位)

函数参数列表中未定义 Python "self"

configuration - Jekyll: 'About' 永久链接无法正常工作

python - “问题”对象没有属性 'strip'

python - 用正则表达式匹配两个 Python 列表,并创建字典输出