2012-02-29 133 views
4

我有一個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

+0

slaveLog是一個全局對象,你每次在runMain()中調用addHandler() – pkit 2012-02-29 13:40:34

+0

謝謝,我已經使用了:「if not len(slaveLog.handlers):」在添加fileHandler之前。我錯誤地認爲記錄模塊不會多次添加相同的fileHandler。 – Jak 2012-02-29 15:19:56

回答

5

logging.getLogger([name])總是返回相同的對象(全局對象),你每次調用runMain時間打電話addHandler操作就可以了。

+0

感謝您的回覆:-)我認爲記錄模塊不會添加已經存在的處理程序?因爲每次調用它時都會使用相同的[名稱],所以我不明白爲什麼它會一直添加另一個處理程序。我甚至可以運行removeHandler()函數,但它不會刪除任何內容。 – Jak 2012-02-29 14:47:29

+2

正如pkit所說,你做錯了。您應該只在一個地方配置日誌記錄(添加處理程序,設置級別等)。看到這篇文章:http://eric.themoritzfamily.com/learning-python-logging.html – 2012-02-29 15:18:04

+0

謝謝我現在擁有它:-) slave.py有時會被自己調用爲腳本,所以在這種情況下,我需要在slave.py中配置日誌記錄。但slave.py也會被master.py多次調用,在這種情況下,日誌記錄已經配置好了,因此slave.py會多次添加相同的fileHandler,從而導致錯誤。 – Jak 2012-02-29 15:27:06

0
#!/usr/bin/env python 
# -*- coding: utf-8 -*- 

import logging 
import logging.handlers 
from logging.config import dictConfig 

logger = logging.getLogger(__name__) 

DEFAULT_LOGGING = { 
    'version': 1, 
    'disable_existing_loggers': False, 
} 
def configure_logging(logfile_path): 
    """ 
    Initialize logging defaults for Project. 

    :param logfile_path: logfile used to the logfile 
    :type logfile_path: string 

    This function does: 

    - Assign INFO and DEBUG level to logger file handler and console handler 

    """ 
    dictConfig(DEFAULT_LOGGING) 

    default_formatter = logging.Formatter(
     "[%(asctime)s] [%(levelname)s] [%(name)s] [%(funcName)s():%(lineno)s] [PID:%(process)d TID:%(thread)d] %(message)s", 
     "%d/%m/%Y %H:%M:%S") 

    file_handler = logging.handlers.RotatingFileHandler(logfile_path, maxBytes=10485760,backupCount=300, encoding='utf-8') 
    file_handler.setLevel(logging.INFO) 

    console_handler = logging.StreamHandler() 
    console_handler.setLevel(logging.DEBUG) 

    file_handler.setFormatter(default_formatter) 
    console_handler.setFormatter(default_formatter) 

    logging.root.setLevel(logging.DEBUG) 
    logging.root.addHandler(file_handler) 
    logging.root.addHandler(console_handler) 



[31/10/2015 22:00:33] [DEBUG] [yourmodulename] [yourfunction_name():9] [PID:61314 TID:140735248744448] this is logger infomation from hello module 

我想你最好在主函數中配置日誌格式。