我正在尝试编写一个模块以在不同的脚本中使用它
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 进行调试时,我确实看到它返回了对象并打印了 dir(log),但我没有在其中看到 Logger 模块。
我是不是漏了什么
最佳答案
log("你好")
这是错误的。 正确的是
log.info("Hello")
日志必须打印日志记录级别,即信息/错误/警告
关于Python Logger 对象不可调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42820860/