记录、删除、检查和修改由 fileConfig() 配置的处理程序
如何使用 fileConfig() 函数删除、检查和修改为记录器配置的处理程序?
对于删除,有 Logger.removeHandler(hdlr) 方法,但是如果它是从文件配置的,如何首先获取该处理程序?
How can I remove, inspect, and modify handlers configured for my loggers using the fileConfig() function?
For removing, there is Logger.removeHandler(hdlr) method, but how do I get the handler in first place if it was configured from file?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
logger.handlers 包含一个记录器的所有处理程序的列表。
logger.handlers
contains a list with all handlers of a logger.此代码将打印所有记录器以及每个记录器的处理程序
这将打印出系统中的记录器和处理程序,包括它们的状态和级别。
这将帮助您调试日志记录问题
This code will print all the loggers and for each logger its handlers
This will print out the loggers and handlers in your system including their states and levels.
This will help you debug your logging issues
Mickey-Perlstein 的回答正是我正在寻找的内容,但列表似乎来自比提供的代码更完整的版本。
这段代码甚至更粗糙,但对于我的使用和理解至关重要,包括根记录器。
输出:
The answer by Mickey-Perlstein was just what I was looking for, but the listing appears to come from a much more complete version than the code provided.
This code is kind of even cruder, but crucially for my use and understanding, includes the root logger.
Output:
另一种方法可能是使用 JSON 或 YAML 配置文件,该文件会加载到字典中,然后您可以在将其传递到 logger.config 之前查看/操作该字典。
Another approach might be to use a JSON or YAML config file which gets loaded into a dictionary which you can then view/manipulate before it's passed to the logger.config.
重新删除处理程序 - 如果它对任何人有帮助,我想删除我创建的所有日志的所有处理程序(我正在学习 Python 日志记录)并提出了这个:
Re removing handlers - if it helps anyone, I wanted to remove all handlers for all the logs I'd created (I was learning about Python logging) and came up with this: