2017-03-15 16 views
0

我試圖寫一個模塊在不同的腳本的Python Logger對象是不可調用的

import logging 
from logging.handlers import RotatingFileHandler 


_logger_name = "Nagios" 
_print_format = "%(asctime)s - %(levelname)s - %(message)s" 
_level = logging.DEBUG 

class Log(): 


    def __init__(self,log_file,logger_name=_logger_name,level=_level): 
     self.log_file = log_file 
     self.logger_name = logger_name 
     self.level = level 

    def getLog(self): 

     """ 
     Return the logging object 

     """ 
     _logger = logging.getLogger(self.logger_name) 
     _logger.setLevel(self.level) 
     _logger.addHandler(self._rotateLog()) 

     return _logger 

    def _rotateLog(self): 

     """ 
     Rotating the log files if it exceed the size 
     """ 
     rh = RotatingFileHandler(self.log_file, 
          maxBytes=20*1024*1024, backupCount=2) 
     formatter = logging.Formatter(_print_format) 
     rh.setFormatter(formatter) 
     return rh 

log = Log("kdfnknf").getLog() 
log("hello") 

我看到下面的錯誤使用它:

Traceback (most recent call last): 
File "nagiosLog.py", line 45, in <module> 
    log("hello") 
TypeError: 'Logger' object is not callable 

任何想法,爲什麼我得到這個錯誤,

當使用pdb進行調試時,我看到它返回的對象和打印目錄(日誌)我沒有看到它的記錄器模塊。

我在這裏缺少

回答

0

東西見logging文檔:

你必須使用一個功能,你不能只是調用記錄儀:

Logger.info(msg, *args, **kwargs)

Logs a message with level INFO on this logger. The arguments are interpreted as for debug().

Logger.warning(msg, *args, **kwargs)

Logs a message with level WARNING on this logger. The arguments are >interpreted as for debug().

所以改爲:

log.info("Test info level logging...") 
相關問題