6

I'm writing to log file using the following code:

import logging
from gmplot import gmplot
logging.basicConfig(filename="sample.log", level=logging.INFO)
logging.debug("This is a debug message")
logging.info("Informational message")
logging.error("An error has happened!")

But then it's impossible to delete this file. how can I 'release' this file?

erez
  • 399
  • 1
  • 6
  • 17

2 Answers2

6

You need to close() your logging:

As explained there: python does not release filehandles to logfile

When your Run class completes, call:

handlers = self.log.handlers[:]
for handler in handlers:
    handler.close()
    self.log.removeHandler(handler)
Luc
  • 1,393
  • 1
  • 6
  • 14
4

simply use logging.shutdown(), when you are done with logging.

Kamlesh
  • 64
  • 1