2014-01-13 37 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'。所以父母大寫,孩子不是。

所以實際上有兩個不同的記錄器不共享相同的層次結構。