Import side effects during registration: how to reset the registration module?

Consider this code:

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

I get:

 print ERROR:root:log 

now, if I turn on the thid-party module at the beginning of the previous code and re-run it, I only get:

 print 

there are some previous questions about this, but here I can not touch on the module that I import.

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: regardless of the module I import, I want clean logging work as expected

Some (non-working) proposed solutions:

 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 following works, but produced some additional errors:

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

I get:

 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") 
+9
source share
3 answers

It depends on what the other module is doing; for example, if it calls logging.disable , then you can call logging.disable(logging.NOTSET) to reset it.

You can try reloading the logging module:

 logging.shutdown() reload(logging) 

The problem is that the third-party module with its own copy of logging is inapplicable, so it can cause more problems later.

+8
source

To completely clear the existing logging configuration from the root logger, this might work:

 root = logging.getLogger() map(root.removeHandler, root.handlers[:]) map(root.removeFilter, root.filters[:]) 

However, this does not mean resetting the value to "default", it clears everything. Then you must add StreamHandler to achieve what you want.

+10
source

A more complete solution that does not invalidate any registrars. It should work if some module does not do something strange, for example, contains a link to a filtering device or handler.

 def reset_logging(): manager = logging.root.manager manager.disabled = logging.NOTSET for logger in manager.loggerDict.values(): if isinstance(logger, logging.Logger): logger.setLevel(logging.NOTSET) logger.propagate = True logger.disabled = False logger.filters.clear() handlers = logger.handlers.copy() for handler in handlers: # Copied from 'logging.shutdown'. try: handler.acquire() handler.flush() handler.close() except (OSError, ValueError): pass finally: handler.release() logger.removeHandler(handler) 

Needless to say, you should configure logging after running reset_logging() .

0
source

All Articles