Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python does not release filehandles to logfile

Tags:

python

logging

I have an application which has to run a number of simulation runs. I want to setup a logging mechanisme where all logrecords are logged in a general.log, and all logs for a simulation run go to run00001.log, .... For this I have defined a class Run. in the __init__() a new filehandle is added for the runlog.

The problem is that the logfiles for the runs never get released, so after a number of runs the available handles are exhausted and the run crashes.

I've set up some routines to test this as follows

main routine

import Model try:     myrun = Model.Run('20130315150340_run_49295')     ha = raw_input('enter')     myrun.log.info("some info") except:     traceback.print_exc(file=sys.stdout)  ha = raw_input('enter3') 

The class Run is defined in module Model as follows

import logging class Run(object):      """ Implements the functionality of a single run. """     def __init__(self, runid):         self.logdir="."         self.runid          = runid         self.logFile        = os.path.join(self.logdir , self.runid + '.log')         self.log            = logging.getLogger('Run'+self.runid)         myformatter         = logging.Formatter('%(asctime)s %(name)-12s %(levelname)-8s %(message)s')         myhandler      = logging.FileHandler(self.logFile)         myhandler.setLevel(logging.INFO)         myhandler.setFormatter(myformatter)         self.log.addHandler(myhandler)  

Then I use the program process explorer to follow the filehandlers. And I see the runlogs appear, but never disappear.

Is there a way I can force this?

like image 554
Bart P. Avatar asked Mar 15 '13 15:03

Bart P.


People also ask

Where does Python logger output to?

logging - Making Python loggers output all messages to stdout in addition to log file - Stack Overflow.

Is logging blocking in Python?

Sometimes you have to get your logging handlers to do their work without blocking the thread you're logging from. This is common in Web applications, though of course it also occurs in other scenarios. So, although not explicitly mentioned yet logging does seem to be blocking. For details see Python Docs.

How do I keep my log in Python?

When you set a logging level in Python using the standard module, you're telling the library you want to handle all events from that level on up. If you set the log level to INFO, it will include INFO, WARNING, ERROR, and CRITICAL messages. NOTSET and DEBUG messages will not be included here.


2 Answers

You need to call .close() on the filehandler.

When your Run class completes, call:

handlers = self.log.handlers[:] for handler in handlers:     handler.close()     self.log.removeHandler(handler) 
like image 67
Martijn Pieters Avatar answered Sep 24 '22 13:09

Martijn Pieters


You can also shutdown the logging completely. In that case, file handles are being released:

logging.shutdown() 

It closes opened handles of all configured logging handlers.

I needed it to be able to delete a log file after a unit test is finished and I was able to delete it right after the call to the logging.shutdown() method.

like image 28
David Ferenczy Rogožan Avatar answered Sep 21 '22 13:09

David Ferenczy Rogožan