【问题标题】:Writing custom log handler in python在 python 中编写自定义日志处理程序
【发布时间】:2016-10-03 15:37:43
【问题描述】:

我正在尝试将 RotatingFile Handler 扩展为 FooBar。

FooBar 类(RotatingFileHandler): def __init__(self, filename, mode='a', maxBytes=0,backupCount=0, encoding=None, delay=0) : RotatingHandler.__init__(self, filename, mode, maxBytes, backupCount, encoding, delay)

我使用配置它

记录 = { “版本”:1, 'disable_existing_loggers':是的, “格式化程序”:{ '详细':{ 'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s' }, '简单的': { '格式': '%(levelname)s %(asctime)s %(message)s' }, }, “处理程序”:{ '文件':{ '级别':'错误', 'class': 'myhandler.FooBar', '格式化程序':'简单', '文件名': '/tmp/cattle.txt', '模式':'一个', “最大字节”:16, “备份计数”:100, }, #-- 剩余部分被截断### logging.config.dictConfig(LOGGING) ### === 错误在这里

当我使用它时;我收到一个错误

文件“/usr/lib/python2.7/logging/config.py”,第 576 行,在配置中 '%r: %s' % (name, e)) ValueError:无法配置处理程序“文件”:未定义全局名称“RotatingHandler”

【问题讨论】:

  • 配置属性class需要处理程序类的完全限定名称,在您的情况下是myhandler.FooBar 对吗?但是你在类实现中import logging.handler.RotatingFileHandler as RotatingFileHandler吗?
  • FooBar 只是一个例子。我想在轮换时更改发出函数的行为并将日志文件复制到 AWS S3
  • 关键是缺少导入 - cf。 ErikR 的回答包括完全跳过推导的提示

标签: python


【解决方案1】:

RotatingHandler 不在范围内,因此您需要这样的东西才能将其纳入范围:

from logging.handlers import RotatingFileHandler

但是,看看这个例子:

How to log everything into a file using RotatingFileHandler by using logging.conf file?

您可能不需要创建自己的类来完成您想做的事情。

【讨论】:

    猜你喜欢
    • 2011-03-08
    • 1970-01-01
    • 1970-01-01
    • 2018-10-29
    • 1970-01-01
    • 1970-01-01
    • 2022-06-21
    • 2016-06-03
    • 1970-01-01
    相关资源
    最近更新 更多