2014-01-13 57 views
0

我想在模块日志记录中设置Python 2.7中的RotatingFileHandler,但无论我做什么,我只能获取写入日志文件的顶级脚本。Python 2.7 RotatingFileHandler无法从模块登录

这是我在我的前Script代码:

import logging 
import Lib.download as d 

LOG_FILE = public_dir + "\script2.log" 

log = logging.getLogger('Script_2') 
log.setLevel(logging.DEBUG) 

rotate_handler = handlers.RotatingFileHandler(LOG_FILE, maxBytes = 5000, backupCount=5) 
log_format = logging.Formatter('%(asctime)s %(name)-8s:%(levelname)s:%(message)s') 
rotate_handler.setFormatter(log_format) 

log.addHandler(rotate_handler) 

log.debug('Launching meat of script_2') 
d.some_function() 
log.info('script_2 completed successfully') 

一个模块 '下载' 代码:

import logging 
log = logging.getLogger('script_2.download') 

def some_function(): 
    log.debug('doing some_function') 

我的输出不会反映模块登录

2014-01-13 12:56:04,428 Script_2:DEBUG:Launching the meat of script_2 
2014-01-13 12:56:05,005 Script_2:INFO:Script_2 completed successfully 

什么我错过了吗?直到我说的RotatingFileHandler ...

我试图从论坛中与其他解决方案,包括Python logging over multiple files

但没有骰子日志模块的工作就好了。任何帮助?

回答

1

不知道如果你想出来,但根据提供的信息,它看起来像你与记录器的名称不匹配。

在您的主脚本中,您将记录器定义为'Script_2',并在您的'download'模块中引用了'script_2.download'。所以父母大写,孩子不是。

所以实际上有两个不同的记录器不共享相同的层次结构。