导入对日志记录的副作用:如何重置日志记录模块? [英] import side effects on logging: how to reset the logging module?

查看:124
本文介绍了导入对日志记录的副作用:如何重置日志记录模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下代码:

import logging
print "print"
logging.error("log")

我得到:

print
ERROR:root:log

现在如果我在上一个代码的开头包含了一个thid-party模块并重新运行它我只得到:

now if I include a thid-party module at the beginning of the previous code and rerun it I get only:

print

之前有一些关于此的问题,但在这里我无法触及我正在导入的模块。

there are some previous question about this, but here I cannot touch the module I'm importing.

第三方模块的代码在这里: http://atlas-sw.cern.ch/cgi-bin/viewcvs-atlas.cgi/offline/DataManagement/DQ2 /dq2.clientapi/lib/dq2/clientapi/DQ2.py?view=markup ,但我的问题更为笼统:独立于我导入的模块,我想要一个干净的日志记录按预期方式工作

The code of the third-party module is here: http://atlas-sw.cern.ch/cgi-bin/viewcvs-atlas.cgi/offline/DataManagement/DQ2/dq2.clientapi/lib/dq2/clientapi/DQ2.py?view=markup, but my question is more general: independently of the module I'm importing I want a clean logging working in the expected way

一些(非工作)建议的解决方案:

from dq2.clientapi.DQ2 import DQ2
import logging
del logging.root.handlers[:]







from dq2.clientapi.DQ2 import DQ2
import logging
logging.disable(logging.NOTSET)







logs = logging.getLogger('root')
logs.error("Some error")






下一个有效,但产生了一些额外的错误:


the next one works, but produced some additional errors:

from dq2.clientapi.DQ2 import DQ2
import logging
reload(logging)

我得到:

print
ERROR:root:log
Error in atexit._run_exitfuncs:
Traceback (most recent call last):
  File "/afs/cern.ch/sw/lcg/external/Python/2.6.5/x86_64-slc5-gcc43-    opt/lib/python2.6/atexit.py", line 24, in _run_exitfuncs
    func(*targs, **kargs)
  File "/afs/cern.ch/sw/lcg/external/Python/2.6.5/x86_64-slc5-gcc43-opt/lib/python2.6/logging/__init__.py", line 1509, in shutdown
    h.close()
  File "/afs/cern.ch/sw/lcg/external/Python/2.6.5/x86_64-slc5-gcc43-opt/lib/python2.6/logging/__init__.py", line 705, in close
    del _handlers[self]
KeyError: <logging.StreamHandler instance at 0x2aea031f7248>
Error in sys.exitfunc:
Traceback (most recent call last):
  File "/afs/cern.ch/sw/lcg/external/Python/2.6.5/x86_64-slc5-gcc43-opt/lib/python2.6/atexit.py", line 24, in _run_exitfuncs
    func(*targs, **kargs)
  File "/afs/cern.ch/sw/lcg/external/Python/2.6.5/x86_64-slc5-gcc43-opt/lib/python2.6/logging/__init__.py", line 1509, in shutdown
    h.close()
  File "/afs/cern.ch/sw/lcg/external/Python/2.6.5/x86_64-slc5-gcc43-opt/lib/python2.6/logging/__init__.py", line 705, in close
    del _handlers[self]
KeyError: <logging.StreamHandler instance at 0x2aea031f7248>







from dq2.clientapi.DQ2 import DQ2
import logging
logger = logging.getLogger(__name__)
ch = logging.StreamHandler()  
logger.addHandler(ch)
logger.error("log")


推荐答案

这取决于另一个模块正在做什么;例如如果它正在调用 logging.disable ,那么你可以调用 logging.disable(logging.NOTSET)来重置它。

It depends on what the other module is doing; e.g. if it's calling logging.disable then you can call logging.disable(logging.NOTSET) to reset it.

您可以尝试重新加载日志模块:

You could try reloading the logging module:

logging.shutdown()
reload(logging)

问题是这会使第三方模块的自身副本 logging 处于不可用状态,因此可能会导致更多问题。

The problem is this will leave the third-party module with its own copy of logging in an unusable state, so could cause more problems later.

这篇关于导入对日志记录的副作用:如何重置日志记录模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆