使用过滤器记录
我正在使用日志记录(导入日志记录
)来记录消息。
在 1 个模块中,我在调试级别记录消息 my_logger.debug('msg')
;
其中一些调试消息来自 function_a()
,其他则来自 function_b()
; 我希望能够根据它们是来自 a 还是来自 b 来启用/禁用日志记录;
我猜我必须使用日志记录的过滤机制。
有人可以告诉我如何使用下面的代码来完成我想要的事情吗?
import logging
logger = logging.getLogger( "module_name" )
def function_a( ... ):
logger.debug( "a message" )
def function_b( ... ):
logger.debug( "another message" )
if __name__ == "__main__":
logging.basicConfig( stream=sys.stderr, level=logging.DEBUG )
#don't want function_a()'s noise -> ....
#somehow filter-out function_a's logging
function_a()
#don't want function_b()'s noise -> ....
#somehow filter-out function_b's logging
function_b()
如果我将这个简单的示例扩展到更多模块和每个模块更多功能,我会担心大量记录器;
我可以将每个模块减少到 1 个记录器吗? 请注意,日志消息是“结构化的”,即如果记录它的函数正在执行一些解析工作,它们都包含前缀 logger.debug("parsing: xxx")
- 我可以吗不知怎的,用一行就可以关闭所有“解析”消息(无论发出消息的模块/函数是什么?)
I'm using Logging (import logging
) to log messages.
Within 1 single module, I am logging messages at the debug level my_logger.debug('msg')
;
Some of these debug messages come from function_a()
and others from function_b()
; I'd like to be able to enable/disable logging based on whether they come from a or from b;
I'm guessing that I have to use Logging's filtering mechanism.
Can someone show me how the code below would need to be instrumented to do what I want?
import logging
logger = logging.getLogger( "module_name" )
def function_a( ... ):
logger.debug( "a message" )
def function_b( ... ):
logger.debug( "another message" )
if __name__ == "__main__":
logging.basicConfig( stream=sys.stderr, level=logging.DEBUG )
#don't want function_a()'s noise -> ....
#somehow filter-out function_a's logging
function_a()
#don't want function_b()'s noise -> ....
#somehow filter-out function_b's logging
function_b()
If I scaled this simple example to more modules and more funcs per module, I'd be concerned about lots of loggers;
Can I keep it down to 1 logger per module? Note that the log messages are "structured", i.e. if the function(s) logging it are doing some parsing work, they all contain a prefix logger.debug("parsing: xxx")
- can I somehow with a single line just shut-off all "parsing" messages (regardless of the module/function emitting the message?)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
只需实现
logging.Filter
的子类即可: http:// /docs.python.org/library/logging.html#filter-objects。 它将有一个方法,filter(record)
,该方法检查日志记录并返回 True 来记录它,或返回 False 来丢弃它。 然后,您可以通过调用addFilter(filter)
方法将过滤器安装在Logger
或Handler
上。示例:
或者类似的东西,无论如何。
Just implement a subclass of
logging.Filter
: http://docs.python.org/library/logging.html#filter-objects. It will have one method,filter(record)
, that examines the log record and returns True to log it or False to discard it. Then you can install the filter on either aLogger
or aHandler
by calling itsaddFilter(filter)
method.Example:
Or something like that, anyway.
不要使用全局。 这是一场等待发生的事故。
您可以为您的记录器指定任何对您有意义的、以“.”分隔的名称。
您可以将它们作为层次结构进行控制。 如果您有名为
abc
的记录器并且abd
,您可以检查ab
的日志记录级别并更改两个记录器。您可以拥有任意数量的记录器——它们很便宜。
最常见的设计模式是每个模块一个记录器。 请参阅命名 Python 记录器
执行此操作。
Do not use global. It's an accident waiting to happen.
You can give your loggers any "."-separated names that are meaningful to you.
You can control them as a hierarchy. If you have loggers named
a.b.c
anda.b.d
, you can check the logging level fora.b
and alter both loggers.You can have any number of loggers -- they're inexpensive.
The most common design pattern is one logger per module. See Naming Python loggers
Do this.
我找到了一种在主脚本中使用函数的更简单的方法:
I found a simpler way using functions in your main script:
我找到了一种更简单的方法,如何使用 sshtunel 模块过滤以下问题的默认日志记录配置,从而抑制 INFO 级别消息。
包含前 2 个不需要的记录的默认报告如下所示:
记录器配置更新:
我满意的结果如下所示:
I've found a bit easier way how to filter default logging configuration on following problem using sshtunel module, supressing INFO level messages.
Default reporting with first 2 undesired records looked as follows:
Logger configuration update:
And result I am happy with looks like this: