0

Dears,

I face an issue with Python: I am creating a log file but once I run my program there is still a link between python and my log file: meaning I can't delete the log file and next log messages will be sent to this log file even if I want to send them elsewhere.

My workaround is to shutdown the kernel and restart but I would like to program it instead of doing it manually. Could you please advise?

My code:

import logging

#initialize the log settings
logging.basicConfig(filename='address.log',level=logging.INFO)
lionrolll
  • 41
  • 6

3 Answers3

0

You need to close the FileHandler after using it. See the related article python does not release filehandles to logfile

Ishan Thilina Somasiri
  • 1,179
  • 1
  • 12
  • 24
  • Hi thanks, I see it's related and that the problem mentioned is similar to mine but I am too novice to figure out how to adapt the solution provided to its specific code to mine... – lionrolll Jun 21 '17 at 07:49
0

Simply use:

logging.shutdown()
Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
Kamlesh
  • 64
  • 1
0

We can do that this way:

log = logging.getLogger()
for hdle in log.handlers[:]:
    if isinstance(hdle,logging.FileHandler): 
        hdle.close()
        log.removeHandler(hdle)

I tried with logging.shutdown() but that doesnt work all the times. You may like to visit this thread as well for more explanation.

Arun
  • 421
  • 3
  • 6