且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

没有root logger的logging.ini文件可以存在吗?

更新时间:2023-08-21 21:16:58

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

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.