使用logger对象而不使用日志记录有什么好处? [英] What is the advantage of having a logger object instead of using logging?

查看:47
本文介绍了使用logger对象而不使用日志记录有什么好处?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

借助Python和 logging 库,您可以通过dict log_config

With Python and the logging library, you can configure logging via a dict log_config and

logging.config.dictConfig(log_config)

您可以通过 logging.info 登录或创建一个记录器对象并使用它.拥有记录器对象有什么好处?

The you can log via logging.info or create a logger object and use that. What is the advantage of having a logger object?

推荐答案

记录器基于其名称(使用"dotted.path"表示法)形成层次结构,其中根记录器位于顶部.创建记录器的标准方法是每个模块都有一个模块,并从模块的 __ name __ 属性中命名,因此,如果您有一个名为"mylib"的程序包,其中包含模块"utils","core"和"api",您将拥有记录器"mylib","mylib.utils","mylib.core"和"mylib.api",其中最后三个是"mylib"的子级(当然是根记录器的子级)

Loggers form a hierarchy based on their names (using "dotted.path" notation), with the root logger on top. The canonical ways to create loggers is to have one per module, named from the module's __name__ attribute so if you have a package named "mylib" with modules "utils", "core" and "api", you'll have loggers "mylib", "mylib.utils", "mylib.core" and "mylib.api", where the three last ones are children of "mylib" (which is of course a child of the root logger).

由此您可以仅配置根记录器,或更具体地配置"mylib"记录器,甚至更具体地配置即"mylib.api"(请注意,默认情况下,记录器会传播到其父级).相反,如果仅在所有软件包中直接使用 logging ,则将无法为每个软件包/模块自定义子记录器.

From this you can configure either only the root logger, or more specifically configure the "mylib" logger, or even more specifically configure ie "mylib.api" (note that by default loggers do propagate to their parents). If instead you only use logging directly in all your package, you won't be able to customize child loggers per package/module.

这里的要点是应该将记录器调用与记录器配置脱钩-库代码定义并调用记录器,配置是使用库的应用程序的职责.显然,原因是库作者不知道哪些应用程序将使用库代码,也不知道应用程序的作者希望如何配置他的记录器.该系统为应用程序的作者(或系统管理员或负责配置/部署应用程序的人员)提供了对记录器配置的全面,细粒度的控制.如果您所有的库代码仅使用root记录器,则应用程序作者/管理员/用户不能对每个库/模块进行不同的设置,并且他会讨厌您这么麻烦(我们不谈论他如何如果您的库尝试以任何方式与记录器配置实际发生冲突,都会感觉到).

The point here is that loggers calls should be decoupled from loggers configuration - library code defines and calls loggers, configuration is the duty of the app using the libraries. The reason is, obviously, that the library author cannot know which apps will use the library code nor how the app's author wants his loggers to be configured. This system gives the app's author (or the sysadmin or whoever is in charge of configuring/deploying the app) full, fine-grained control over logger's configuration. If all your library code only use the root logger then the app author/admin/user cannot have different settings per library/module, and (s)he will hate you for being so intrusive (and let's not talk about how (s)he will feel if your library tries to actually mess with the loggers configuration in any way).

长话短说:坚持理智的惯例,请在模块中使用 logger = logging.getLogger(__ name __),不要strong>尝试配置您的库代码中的日志记录,您的库用户会很高兴.

To make a long story short: stick to the sane conventions, use logger = logging.getLogger(__name__) in your modules, do not try to configure logging in your library code, and your library users will be happy.

如您在评论中所述,如果应用在导入lib 后未配置未设置 disable_existing_loggers 设置为False.就我而言,应用程序的作者负责在导入其他内容之前配置日志记录和/或将 disable_existing_loggers 设置为False.无论如何,首先配置日志记录是恕我直言的一个好主意,因为它将允许库代码在导入时记录最终的问题...

EDIT : as you mention in a comment, declaring loggers at the module level can cause problems if the app configures the logging after importing your lib and does not set disable_existing_loggers to False. As far as I'm concerned it's the app's author responsability to either configure logging before import anything else and/or to set disable_existing_loggers to False. Configuring the logging first is IMHO a good idea anyway since it will allow library code to log eventual issues at import time...

这篇关于使用logger对象而不使用日志记录有什么好处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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