使用 python 日志记录包,如何将附加格式插入到具有自己的格式化程序的多个记录器处理程序中?
我有一个带有多个处理程序的记录器,它们有自己的格式化程序。现在我想添加一个缩进功能,并在运行时控制缩进级别。我希望来自所有处理程序的消息都能获得此缩进。我尝试将其创建为过滤器,但发现我似乎无法更改消息内容。然后我尝试将它作为格式化程序,但每个处理程序只能有一个。如何在不显式更改每个处理程序的格式化程序的情况下添加此类缩进?
我应该提到,我拥有的格式化程序之一是一个为输出添加颜色的类。它不是一个简单的格式字符串。
此外,我正在使用配置文件。理想情况下,我希望能够主要从那里驱动它。但是,我需要修改缩进格式化程序的状态(例如设置缩进级别),但我不知道如何访问该特定格式化程序,因为没有 logger.getFormatter("by_name")
方法。
为了澄清这一点,我需要访问特定的格式化程序实例,本质上是动态调整格式。该实例已由文件中的logging.config 创建。我没有找到任何访问器方法可以让我获得特定格式化程序的名称。
I have a single logger with multiple handlers, which have their own formatters. Now I want to add an indenting feature, with the indent level controlled at runtime. I want messages from all the handlers to get this indent. I've tried to create it as a filter, but found that I seem to be unable to alter the message content. Then I've tried it as a formatter, but I can only have one per handler. How can I add such indentation without explicitly changing the formatters of every handler?
I should mention that one of the formatters I have is a class that adds color to the output. It is not a simple format string.
In addition, I am using a config file. Ideally, I'd like to be able to drive this mostly from there. However, I need to modify the state of the indent formatter (e.g. set indent level), but I don't know how to get to that particular formatter as there's no logger.getFormatter("by_name")
method.
To clarify, I need to access the specific formatter instance, essentially to adjust the format on the fly. The instance has been created by logging.config from the file. I don't find any accessor methods that would allow me to get the particular formatter given its name.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
结果:
我不确定我是否理解你的第二个问题。
result:
I'm not sure I understand your second question.
这是另一种,虽然很老套,但很简单。我的所有处理程序的消息始终以消息级别字符串开头。只需在每次缩进更改时修改这些该死的字符串即可:
(有关缩进功能的内容,请参阅我的其他答案)
现在压头可以是一个独立的类,而不会干扰日志记录过程的细节。只要消息包含级别字符串,就会出现缩进,即使在它之前有一些内容。一般来说并不理想,但可能对我有用。
有人有更多适用于任何消息格式的想法吗?
Here's another, hacky, but simple one. My messages for all handlers always start with a message level string. Just modify those darn strings on every indent change:
(see my other answer for the stuff that surrounds indent function)
Now the indenter can be an independent class without messing with the details of logging process. As long as the message includes the level string, the indent will be there, even if some stuff is coming before it. Not ideal in general, but may work for me.
Anybody has more ideas that work for any msg format?
好吧,这是一种几乎可以满足我需要的方法。子类化 LogRecord 以覆盖 getMessage 以插入缩进,并子类化记录器以使用它进行 makeRecord:
结果是(实际上是彩色的):
不知怎的,日志级别字符串仍然潜到前面,所以它不完全是我想要的。此外,这很尴尬 - 对于这样一个简单的功能来说太多了:(
更好的想法?
Ok, here's one way that gets me ALMOST what I need. Subclass the LogRecord to overwrite getMessage for inserting indent and subclass logger to makeRecord with it:
The result is (colored in reality):
Somehow the log level string still sneaks to the front, so it is not quite what I want. Besides, this is awkward - too much for such a simple feature :(
Better ideas?