Python Logger 对象不可调用

标签 python

我正在尝试编写一个模块以在不同的脚本中使用它

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/

相关文章:

python - 以平衡权重的 block 拆分列表

python - 读取文件的多种方式?

Python datetime 和 pandas 为同一日期提供不同的时间戳

python - PySpark/Aws Glue 中的性能问题

python - 从 or-tools 获得更多输出

python - 获取时间戳在不规则时间间隔内的行 pandas (Time Series)

python - graphviz Python Node Shapes - 节点中的饼图

python - 逃生舱 Google Foobar 挑战 |最大流量问题

python - 在seaborn箱线图上叠加额外的百分位标记时出现问题

python - Django ModelAdmin get_form() 没有设置字段属性