Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python logging wont shutdown

Tags:

python

logging

I have been learning the python logging module but have been having problems getting the logging to shutdown after it's done. Here is an example -

import logging

log = logging.getLogger()
log.setLevel(logging.INFO)
handler = logging.FileHandler('test.log')
handler.setLevel(logging.INFO)
formatter = logging.Formatter(
            fmt='%(asctime)s %(levelname)s: %(message)s',
            datefmt='%Y-%m-%d %H:%M:%S'
            )
handler.setFormatter(formatter)
log.addHandler(handler)

log.info('log file is open')  
logging.shutdown()
log.info('log file should be closed')

But the module is stilling logging after the logging.shutdown() as the log file looks like this -

# cat test.log
2014-07-17 19:39:35 INFO: log file is open
2014-07-17 19:39:35 INFO: log file should be closed

According to the documentation this command should "perform an orderly shutdown by flushing and closing all handlers". Should I be doing something else to close the log file ?

like image 446
nerdofnetworks Avatar asked Jul 18 '14 00:07

nerdofnetworks


People also ask

Why is the module still logging after shutdown?

But the module is stilling logging after the logging.shutdown () as the log file looks like this - According to the documentation this command should "perform an orderly shutdown by flushing and closing all handlers".

What to do after shutdown in Python?

The Python Doc for shutdown specifies that: This should be called at application exit and no further use of the logging system should be made after this call. It does not mean that the logger object cannot be used afterwards. After calling shutdown, simply don't try to log anything else using that logger object and that should be fine.

How many code examples are there for logging shutdown?

The following are 30 code examples for showing how to use logging.shutdown () . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

Can I use the logger object after shutdown?

It does not mean that the logger object cannot be used afterwards. After calling shutdown, simply don't try to log anything else using that logger object and that should be fine. Show activity on this post. I had the same probleme with the creation of a new logger with every call of the function.


4 Answers

So I found that the file handler for logging is not completely going away by using shutdown(). The best way seems to be to manually remove the file handler -

def main():
    log = logging.getLogger()
    log.setLevel(logging.INFO)
    fh = logging.FileHandler(filename='test.log')
    fh.setLevel(logging.INFO)
    formatter = logging.Formatter(
                    fmt='%(asctime)s %(levelname)s: %(message)s',
                    datefmt='%Y-%m-%d %H:%M:%S'
                    )
    fh.setFormatter(formatter)
    log.addHandler(fh)

    log.info('-------Start--------')
    log.info('this function is doing something')
    log.info('this function is finished')
    log.removeHandler(fh) <--------------------------Add this line
    del log,fh
like image 59
nerdofnetworks Avatar answered Oct 08 '22 03:10

nerdofnetworks


I had the same probleme with the creation of a new logger with every call of the function. A solution that i found is to clear all handlers after executing the code

log.handlers.clear()

Don't know if it is the correct way but it works for me.

like image 30
Robin Avatar answered Oct 08 '22 03:10

Robin


I had this problem while writing a tKinter application. I had extensive logging as various controls were being used and every time I (for instance) pressed a certain button multiple times consecutively, the number of times a line was logged was equal to the number of times I pressed the button consecutively.

I added: log.handlers.clear() at the end of main() and it cleared the issue up.

like image 43
cromastro Avatar answered Oct 08 '22 02:10

cromastro


The Python Doc for shutdown specifies that:

This should be called at application exit and no further use of the logging system should be made after this call.

It does not mean that the logger object cannot be used afterwards. After calling shutdown, simply don't try to log anything else using that logger object and that should be fine.

like image 43
khampson Avatar answered Oct 08 '22 03:10

khampson