在Python中使用日志模块的奇怪问题

2024-04-18 21:21:53 发布

您现在位置:Python中文网/ 问答频道 /正文

在调用正在处理的应用程序中的另一个模块后记录数据时,我似乎遇到了问题。我想帮助你了解这里可能发生的事情。你知道吗

为了复制这个问题,我开发了以下脚本。。。你知道吗

#!/usr/bin/python

import sys
import logging
from oletools.olevba import VBA_Parser, VBA_Scanner
from cloghandler import ConcurrentRotatingFileHandler

# set up logger for application
dbg_h = logging.getLogger('dbg_log')
dbglog = '%s' % 'dbg.log'
dbg_rotateHandler = ConcurrentRotatingFileHandler(dbglog, "a")
dbg_h.addHandler(dbg_rotateHandler)
dbg_h.setLevel(logging.ERROR)

# read some document as a buffer
buff = sys.stdin.read()

# generate issue
dbg_h.error('Before call to module....')
vba = VBA_Parser('None', data=buff)
dbg_h.error('After call to module....')

当我运行这个时,我得到以下结果。。。你知道吗

cat somedocument.doc | ./replicate.py
ERROR:dbg_log:After call to module....

出于某种原因,我的最后一次dbg\u h logger写入尝试是将输出写入控制台以及我的日志数据库日志文件?这似乎只发生在调用VBA\u解析器之后。你知道吗

cat dbg.log
Before call to module....
After call to module....

有人知道为什么会这样吗?我回顾了source code of olevba,没有看到任何特别突出我的东西。你知道吗

这是我应该向模块作者提出的问题吗?或者我在使用cloghandler的时候做错了什么?你知道吗


Tags: 模块tofromimportlogparserloggingsys
1条回答
网友
1楼 · 发布于 2024-04-18 21:21:53

尽管对logging.debug(...)logging.error(...)等的调用,但oletools代码基中充斥着对根记录器的调用。由于作者没有费心配置根记录器,默认行为是转储到sys.stderr。因为从命令行运行时sys.stderr默认为控制台,所以您可以看到所看到的内容。你知道吗

您应该联系oletools的作者,因为他们没有有效地使用日志系统。理想情况下,他们将使用一个命名的记录器并将消息推送到该记录器。作为抑制消息的解决方法,可以将根记录器配置为使用处理程序。你知道吗

# Set a handler
logger.root.addHandler(dbg_rotateHandler)

请注意,这可能会导致重复的日志消息。你知道吗

相关问题 更多 >