Python使用同一记录器记录多个文件 [英] Python logging multiple files using the same logger

查看:79
本文介绍了Python使用同一记录器记录多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的情况:我想记录my_module的活动.需要执行此操作,具体取决于执行的方法(例如INPUT和OUTPUT)到两个不同的文件.

This is my scenario: I want to log my_module's activity. This needs to be done, depending on the method executed (let's say, INPUT and OUTPUT), to two different files.

因此,我有两个处理程序,每个处理程序都指向一个具有相同日志级别的不同文件(my_in_.log和my_out_.log).我想知道是否可以使用同一记录器来实现这一目标,或者我必须定义两个记录器.我的配置是:

So I have two Handlers, each one point to a different file (my_in_.log & my_out_.log), with the same log level. I would like to know if I can use the same logger to achieve this or I have to define two loggers. My config is:

[loggers]
keys=root, my_log

[handlers]
keys=my_in_hand, my_out_hand

[formatters]
keys=generic_form


...


[logger_my_log]
level=NOTSET
handlers=my_in_hand, my_out_hand
qualname=ws_log

[handler_my_in_hand]
class=handlers.TimeRotatingFileHandler
level=NOTSET
formatter=generic_form
args=('my_in_.log', 'h', 1, 0, None, False, True)

[handler_my_out_hand]
class=handlers.TimeRotatingFileHandler
level=NOTSET
formatter=generic_form
args=('my_out_.log', 'h', 1, 0, None, False, True)

我是否必须为每个处理程序/目标定义一个记录器(因为我想在不同的文件中记录不同的信息)?有没有办法向记录器指示哪个处理程序将执行此操作?我的意思是,我有一个记录器有两个处理程序,然后仅选择一个处理程序来记录一种方法.

Do I have to define a logger per handler/destination (because I want to log different information in different files)? Is there a way to indicate to the logger which handler will do this? I mean, I have two handlers for one logger, then choose only one handler to log one method.

推荐答案

最后,我决定定义两个记录器,因为:

Finally I decided to define two loggers, because:

  • 它们用于不同的目的.在我的情况下,一个将输入请求记录到Web服务,另一个记录响应.他们使用了不同的文件

  • They are for different purposses. In my case, one logs input request to a web service, and the other one logs the response. And they use different files to it

我正在使用前端Web服务中的日志记录配置文件.正如@mike所说,在记录消息之前添加/删除处理程序不是正确的方法.谢谢@drekyn!

I'm using a logging config file, in a frontal web service. Adding/removing handlers before logging messages is not the right approach, as @mike said. Thx to @drekyn too!

这是我的日志记录配置文件,仅供参考,如果有人感兴趣:

Here is my logging config file, just for reference if anyone is interested in:

[loggers]
keys=root, ws_in_log, ws_out_log

[handlers]
keys=consoleHandler, ws_in_hand, ws_out_hand

[formatters]
keys=generic_form

[logger_root]
handlers=consoleHandler
level=NOTSET

[logger_ws_in_log]
level=NOTSET
handlers=ws_in_hand
qualname=ws_in_log

[logger_ws_out_log]
level=NOTSET
handlers=ws_out_hand
qualname=ws_out_log

[handler_ws_in_hand]
class=logging.handlers.TimedRotatingFileHandler
level=NOTSET
formatter=generic_form
args=('/path/ws_in_.log', 'h', 1, 0, None, False, True)

[handler_ws_out_hand]
class=logging.handlers.TimedRotatingFileHandler
level=NOTSET
formatter=generic_form
args=('/path/em/ws_out_.log', 'h', 1, 0, None, False, True)

[handler_consoleHandler]
class=StreamHandler
level=DEBUG
formatter=generic_form
args=(sys.stdout,)

[formatter_generic_form]
format='%(asctime)s - %(levelname)s - %(message)s'
datefmt='%Y-%m-%d %H:%M:%S'
class=

再见!

这篇关于Python使用同一记录器记录多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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