我有一個Python日誌記錄模塊的問題,我不確定我是否在這裏做一些愚蠢的事情,但我有兩個Python腳本,一個(master.py)調用另一個(slave)。 PY)。他們都記錄到單獨的日誌文件,但被調用的第二個腳本(slave.py)似乎遞歸地記錄!Python日誌多個文件
任何人都可以看到我在做什麼錯在這裏?
這裏是我的代碼:
# master.py
import sys
import logging
import slave
masterLog = logging.getLogger('master')
masterLog.setLevel(logging.DEBUG)
masterHandler = logging.FileHandler('master.log')
formatter = logging.Formatter('%(levelname)s: %(asctime)s %(funcName)s(%(lineno)d) -- %(message)s', datefmt = '%Y-%m-%d %H:%M:%S')
masterHandler.setFormatter(formatter)
masterLog.addHandler(masterHandler)
masterLog.info('This is masterLog running on master.py')
print 'master.py has: ', len(masterLog.handlers), 'handlers'
for i in range(1,6):
masterLog.info('Running slave %s' % i)
slave.runMain()
# slave.py
import sys
import logging
def runMain():
slaveLog = logging.getLogger('slave')
slaveLog.setLevel(logging.DEBUG)
slaveHandler = logging.FileHandler('slave.log')
formatter = logging.Formatter('%(levelname)s: %(asctime)s %(funcName)s(%(lineno)d) -- %(message)s', datefmt = '%Y-%m-%d %H:%M:%S')
slaveHandler.setFormatter(formatter)
slaveLog.addHandler(slaveHandler)
slaveLog.info('This is slaveLog running on slave.py')
print 'slave.py has: ', len(slaveLog.handlers), 'handlers'
if __name__ == '__main__':
runMain()
這裏是輸出:
master.py has: 1 handlers
slave.py has: 1 handlers
slave.py has: 2 handlers
slave.py has: 3 handlers
slave.py has: 4 handlers
slave.py has: 5 handlers
而且MASTER.LOG:
INFO: 2012-02-29 13:26:49 <module>(13) -- This is masterLog running on master.py
INFO: 2012-02-29 13:26:49 <module>(17) -- Running slave 1
INFO: 2012-02-29 13:26:49 <module>(17) -- Running slave 2
INFO: 2012-02-29 13:26:49 <module>(17) -- Running slave 3
INFO: 2012-02-29 13:26:49 <module>(17) -- Running slave 4
INFO: 2012-02-29 13:26:49 <module>(17) -- Running slave 5
而SLAVE.LOG:
INFO: 2012-02-29 13:26:49 runMain(13) -- This is slaveLog running on slave.py
INFO: 2012-02-29 13:26:49 runMain(13) -- This is slaveLog running on slave.py
INFO: 2012-02-29 13:26:49 runMain(13) -- This is slaveLog running on slave.py
INFO: 2012-02-29 13:26:49 runMain(13) -- This is slaveLog running on slave.py
INFO: 2012-02-29 13:26:49 runMain(13) -- This is slaveLog running on slave.py
INFO: 2012-02-29 13:26:49 runMain(13) -- This is slaveLog running on slave.py
INFO: 2012-02-29 13:26:49 runMain(13) -- This is slaveLog running on slave.py
INFO: 2012-02-29 13:26:49 runMain(13) -- This is slaveLog running on slave.py
INFO: 2012-02-29 13:26:49 runMain(13) -- This is slaveLog running on slave.py
INFO: 2012-02-29 13:26:49 runMain(13) -- This is slaveLog running on slave.py
INFO: 2012-02-29 13:26:49 runMain(13) -- This is slaveLog running on slave.py
INFO: 2012-02-29 13:26:49 runMain(13) -- This is slaveLog running on slave.py
INFO: 2012-02-29 13:26:49 runMain(13) -- This is slaveLog running on slave.py
INFO: 2012-02-29 13:26:49 runMain(13) -- This is slaveLog running on slave.py
INFO: 2012-02-29 13:26:49 runMain(13) -- This is slaveLog running on slave.py
從屬腳本似乎它的每一個調用的時候添加新的文件處理程序。 slave.log文件中只應該有5個條目,但每次slave.py被稱爲記錄器輸出到每個文件處理程序!
感謝, 的Jak
slaveLog是一個全局對象,你每次在runMain()中調用addHandler() – pkit 2012-02-29 13:40:34
謝謝,我已經使用了:「if not len(slaveLog.handlers):」在添加fileHandler之前。我錯誤地認爲記錄模塊不會多次添加相同的fileHandler。 – Jak 2012-02-29 15:19:56