Python日志劫持子模块日志到另一个文件(Pylons)

时间:2011-02-17 08:14:29

标签: python logging pylons

我有一个pylons应用myapp,我需要将来自子模块myapp.lib.submodule的所有日志消息写入不同的日志文件,而也要写入主日志文件中

这是我当前的ini文件,这样我就可以在 accesslog和submodulelog文件中获取子模块消息:

...
# Logging configuration
[loggers]
keys = root, routes, myapp, sqlalchemy, submodule

[handlers]
keys = console, accesslog, submodulelog

[formatters]
keys = generic

[logger_root]
level = INFO
handlers = accesslog

[logger_routes]
...

[logger_myapp]
level = INFO
handlers = accesslog
qualname = myapp.controllers

[logger_submodule]
level = INFO
handlers = submodulelog
qualname = myapp.lib.submodule

[logger_sqlalchemy]
....

[handler_console]
class = StreamHandler
args = (sys.stderr,)
level = NOTSET
formatter = generic

[handler_accesslog]
class = handlers.TimedRotatingFileHandler
args = ('/tmp/myapp.log', 'midnight', 1, 30, 'utf-8')
level = INFO
formatter = generic

[handler_submodulelog]
class = handlers.TimedRotatingFileHandler
args = ('/tmp/submodule.log', 'midnight', 1, 30, 'utf-8')
level = INFO
formatter = generic

[formatter_generic]
....

1 个答案:

答案 0 :(得分:2)

您可以使用子模块记录器的传播标志来停止将消息传递给祖先记录器,如下所示:

[logger_submodule]                                                                                                                                           
level = INFO                                                                                                                                                 
handlers = submodulelog                                                                                                                                      
qualname = myapp.lib.submodule                                                                                                                               
propagate=0