Python,日志记录:使用带有字典配置的自定义处理程序?

发布于 2024-10-22 07:23:29 字数 1944 浏览 7 评论 0原文

这是关于 Python 3.2 (GNU/Linux x86_64) 上的日志记录模块:是否可以使用字典配置设置自定义处理程序? 这是我正在尝试的代码:

import logging
import logging.config

class CustomHandler(logging.StreamHandler):
    pass

logconfig = {
    'version': 1,
    'handlers': {
        'console': {
            'class': 'CustomHandler',
        }
    },
    'loggers': {
        'custom': {
            'handlers': ['console'],
        }
    }
}
logging.config.dictConfig(logconfig)
logger = logging.getLogger('custom')
logger.error('Error message')

这当然不起作用。这是输出:

Traceback (most recent call last):
  File "/usr/lib/python3.2/logging/config.py", line 390, in resolve
    found = self.importer(used)
ImportError: No module named CustomHandler

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/lib/python3.2/logging/config.py", line 569, in configure
    handler = self.configure_handler(handlers[name])
  File "/usr/lib/python3.2/logging/config.py", line 698, in configure_handler
    klass = self.resolve(config.pop('class'))
  File "/usr/lib/python3.2/logging/config.py", line 403, in resolve
    raise v
  File "/usr/lib/python3.2/logging/config.py", line 390, in resolve
    found = self.importer(used)
ValueError: Cannot resolve 'CustomHandler': No module named CustomHandler

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "./prova.py", line 91, in <module>
    logging.config.dictConfig(logconfig)
  File "/usr/lib/python3.2/logging/config.py", line 777, in dictConfig
    dictConfigClass(config).configure()
  File "/usr/lib/python3.2/logging/config.py", line 574, in configure
    '%r: %s' % (name, e))
ValueError: Unable to configure handler 'console': Cannot resolve 'CustomHandler': No module named CustomHandler

源代码中有一个 importer 方法,我真的不明白......有什么想法吗?

谢谢你!

this is about the logging module on Python 3.2 (GNU/Linux x86_64): is it possible to set a custom handler with dictionary configuration?
This is the code I'm trying:

import logging
import logging.config

class CustomHandler(logging.StreamHandler):
    pass

logconfig = {
    'version': 1,
    'handlers': {
        'console': {
            'class': 'CustomHandler',
        }
    },
    'loggers': {
        'custom': {
            'handlers': ['console'],
        }
    }
}
logging.config.dictConfig(logconfig)
logger = logging.getLogger('custom')
logger.error('Error message')

Which of course does not work. This is the output:

Traceback (most recent call last):
  File "/usr/lib/python3.2/logging/config.py", line 390, in resolve
    found = self.importer(used)
ImportError: No module named CustomHandler

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/lib/python3.2/logging/config.py", line 569, in configure
    handler = self.configure_handler(handlers[name])
  File "/usr/lib/python3.2/logging/config.py", line 698, in configure_handler
    klass = self.resolve(config.pop('class'))
  File "/usr/lib/python3.2/logging/config.py", line 403, in resolve
    raise v
  File "/usr/lib/python3.2/logging/config.py", line 390, in resolve
    found = self.importer(used)
ValueError: Cannot resolve 'CustomHandler': No module named CustomHandler

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "./prova.py", line 91, in <module>
    logging.config.dictConfig(logconfig)
  File "/usr/lib/python3.2/logging/config.py", line 777, in dictConfig
    dictConfigClass(config).configure()
  File "/usr/lib/python3.2/logging/config.py", line 574, in configure
    '%r: %s' % (name, e))
ValueError: Unable to configure handler 'console': Cannot resolve 'CustomHandler': No module named CustomHandler

There is that importer method in the source which I really don't understand... Any idea?

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

桃扇骨 2024-10-29 07:23:29

您需要告诉它在哪里可以找到 CustomHandler 类。该字符串应包含可以找到它的模块(可能还有包)。如果直接运行此脚本,则可以使用__main__.CustomHandler。否则,请使用 your_module.CustomHandler,其中将 your_module 替换为包含该类的模块的名称。

You need to tell it where to find the CustomHandler class. The string should contain the module (and possibly package) where it can be found. If you are running this script directly, you can use __main__.CustomHandler. Otherwise, use your_module.CustomHandler, Where you replace your_module with the name of the module containing the class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文