Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

import side effects on logging: how to reset the logging module?

Tags:

Consider this code:

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

I get:

print ERROR:root:log 

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.

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

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 next one 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") 
like image 809
Ruggero Turra Avatar asked Aug 20 '12 08:08

Ruggero Turra


People also ask

How do I reset my logger?

To reset the processor, simply power cycle the data logger. This resets its short-term memory, restarts the current program, sets variables to their starting values, and clears communications buffers. This does not clear data tables but may result in a skipped record.

How do you clear a log file in Python?

You'd have to signal to the daemon that the log file's been removed so it can close/reopen the log file handle and start using the new log file. Otherwise it'll just keep writing to the old one, which will still exist somewhere until all filehandles on it are closed. Give mode="w" to logging. FileHandler .

What does the logging module do?

Standard Library Logging Module The module provides a way for applications to configure different log handlers and a way of routing log messages to these handlers. This allows for a highly flexible configuration that can deal with a lot of different use cases.

What is the default level in logging module?

By default, the root log level is WARN, so every log with lower level (for example via logging.info("info") ) will be ignored. Another particularity of the root logger is that its default handler will be created the first time a log with a level greater than WARN is logged.


1 Answers

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:

from importlib import reload logging.shutdown() reload(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.

like image 96
ecatmur Avatar answered Oct 21 '22 19:10

ecatmur