【问题标题】:Use fileConfig to configure custom handlers in Python使用 fileConfig 在 Python 中配置自定义处理程序
【发布时间】:2015-03-19 05:50:14
【问题描述】:

我正在使用配置文件在 Python 应用程序中配置我的记录器。这是文件:

[loggers]
keys=root

[logger_root]
level=INFO
handlers=console

[handlers]
keys=console,file_rotating

[handler_console]
class=StreamHandler
level=WARNING
formatter=console
args=(sys.stderr,)

[handler_file_rotating]
class=TimeRotatingFileHandler
level=DEBUG
formatter=file
args=('../logs/twicker.log', 'd', 1, 5)

[formatters]
keys=console,file

[formatter_console]
format=%(levelname)s - %(message)s

[formatter_file]
format=%(asctime)s - %(levelname)s - %(module)s - %(message)s

我的问题是 TimeRotatingFileHandler。每次我运行应用程序时,我都会收到下一个错误:

ImportError: 没有名为“TimeRotatingFileHandler”的模块

我做错了什么?我也尝试将类行更改为class=handlers.TimeRotatingFileHandler,但在这种情况下,我会收到下一个错误:

ImportError: 没有名为“处理程序”的模块

【问题讨论】:

  • 如果您能选择正确的答案将不胜感激...

标签: python python-3.x logging


【解决方案1】:

如果您决定创建一个自定义处理程序,则对此进行一些扩展,您只需在代码顶部附近定义该处理程序,然后将其定义为 logging.handlers 中的对象。

例子:

class MyCustomRotatingClass(logging.handlers.RotatingFileHandler):
   """custom handler that queues messages
      to be uploaded in batches to the portal
      in a background thread
   """
   def __init__(self, basedir, mode='a', maxBytes=0, backupCount=0, encoding=None, delay=0):
       logging.handlers.RotatingFileHandler.__init__(self, 'TestSavvyExecute.log','a',250000,40,'utf-8',0)
       self.maxBytes = maxBytes

   def emit(self, record):
       try:
           if logging.handlers.RotatingFileHandler.shouldRollover(self, record):
               logging.handlers.RotatingFileHandler.doRollover(self)

           #ASCII characters use 1 byte each
           if len(record.msg) > self.maxBytes:
               oldMsg = record.msg

               record.msg = record.msg[0:self.maxBytes]
               logging.FileHandler.emit(self, record)

               record.msg = oldMsg[self.maxBytes + 1:]
               self.emit(record)
           else:
              logging.FileHandler.emit(self, record)
       except (KeyboardInterrupt, SystemExit):
           raise
       except:
           logging.handlers.RotatingFileHandler.handleError(self, record)          

logging.handlers.MyCustomRotatingClass = MyCustomRotatingClass
logging.config.fileConfig('translator.log.config')

现在您可以轻松地在配置文件中引用它。

[handler_rollinglog]
class=handlers.MyCustomRotatingClass

【讨论】:

  • 我尝试在此 BufferingSMTPHandler 上方使用您的示例,但当我将其切换到 fileConfig 时,它似乎没有发送电子邮件。有任何想法吗? gist.github.com/anonymous/1379446
【解决方案2】:

我在使用dictConfig 时遇到了同样的问题,我的解决方案是像这样完全限定模块路径:

[handler_file_rotating]
class=logging.handlers.TimeRotatingFileHandler
level=DEBUG
formatter=file
args=('../logs/twicker.log', 'd', 1, 5)

您可能想尝试一下

【讨论】:

    【解决方案3】:

    class=logging 模块的命名空间中进行评估,默认情况下它没有绑定到handlers。所以你可以这样做

    import logging, logging.handlers
    logging.handlers = logging.handlers
    

    在调用fileConfig() 之前,然后class=handlers.TimedRotatingHandler 应该可以工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-23
      • 2010-10-20
      • 2013-01-21
      • 2011-05-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多