我正在扯头发。
我需要一个系统日志处理程序。
我找到了 this : 在 StackOverflow page并发现该方法独立运行时效果很好,可以与该系统的系统日志进行通信。
但是,我不知道如何在我的日志记录配置文件中引用它。我使用的是 Python 2.6.6,所以我无法使用 dict 配置。我必须使用文件配置。根据文档,类名必须是相对于日志记录或来自 python 路径的完整规范。
这是我的配置文件的一部分:
[handler_syslog]
class=logext.HDSysLogHandler
level=WARN
formatter=timestamp
args=(7, )
logext 是一个比我的 main 深一层的包。
它包含 3 个 python 文件和编译文件:
[logext]$ ls -al
total 32
drwxr-x---. 2 sc1478 dev 4096 Apr 25 11:50 .
drwxr-x---. 3 sc1478 dev 4096 Apr 25 11:50 ..
-rw-r-----. 1 sc1478 dev 243 Apr 25 11:50 HDSysLogHandler.py
-rw-r-----. 1 sc1478 dev 630 Apr 25 11:50 HDSysLogHandler.pyc
-rw-r-----. 1 sc1478 dev 2 Apr 25 11:50 __init__.py
-rw-r-----. 1 sc1478 dev 132 Apr 25 11:50 __init__.pyc
-rw-r-----. 1 sc1478 dev 2426 Apr 25 11:50 syslog_bridge.py
-rw-r-----. 1 sc1478 dev 2386 Apr 25 11:50 syslog_bridge.pyc
init.py是空的,只是为了打个包。
syslog.bridge.py 基本上就是上面提到的文件,对构造函数进行了一些小改动,使其更加灵活并适用于 Python 2.6
class SysLogLibHandler(logging.Handler):
...
def __init__(self, identifier, n):
try:
syslog.openlog(identifier, syslog.LOG_PID, self.FACILITY[n])
except Exception , err:
print err
raise
# We got it
logging.Handler.__init__(self)
最后HDSysLogHandler.py如下:
from syslog_bridge import SysLogLibHandler
class HDSysLogHandler(SysLogLibHandler):
def __init__(self, local_facility):
SysLogLibHandler.__init__(self, "hdaudio", local_facility)
主要模块的相关部分是:
import logging
import logging.config
import logext
...
logging.config.fileConfig('logging.conf')
当我的主应用程序运行时,我得到这个:
Traceback (most recent call last):
File "Main.py", line 23, in <module>
logging.config.fileConfig('logging.conf')
File "/usr/lib64/python2.6/logging/config.py", line 84, in fileConfig
handlers = _install_handlers(cp, formatters)
File "/usr/lib64/python2.6/logging/config.py", line 162, in _install_handlers
h = klass(*args)
TypeError: 'module' object is not callable
这是想告诉我什么?什么模块不可调用?谁在尝试“调用它”?
找到自制的日志记录处理程序并在日志记录配置文件中指定它的正确方法是什么?
最佳答案
我认为您看到此错误是因为
class=logext.HDSysLogHandler
将类指定为 logext.HDSysLogHandler
,这是一个模块,而不是类。而是尝试
class=logext.HDSysLogHandler.HDSysLogHandler
因为这指定了 HDSysLogHandler
类,它位于 logext.HDSysLogHandler
模块中。
关于Python 日志记录配置文件模块和包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23300309/