6

What I want: My python script runs, output logging messages to both the console and to a file.

Once the python script finishes running, I want to be able to delete/edit the logging file. I am using Spyder IDE on Windows7.

Example code:

import logging

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)

hdlr = logging.FileHandler("/Users/mds/Dropbox/_python/logger-debug.txt")
logger.addHandler(hdlr) 

logger.error("Am I duplicating error entries?")

hdlr.close()

Problems I'm having:

  1. After the script finishes running, there is still a lock on the file enter image description here

  2. Each time I run the script the log file grows many duplicate entries.

first time I run the script:

console:

runfile('C:/Users/mds/Dropbox/_python/logger-debugger.py', wdir='C:/Users/mds/Dropbox/_python')
ERROR:__main__:Am I duplicating error entries?

logger-debug.txt:

Am I duplicating error entries?

Second time I run the script: console:

runfile('C:/Users/mds/Dropbox/_python/logger-debugger.py', wdir='C:/Users/mds/Dropbox/_python')
ERROR:__main__:Am I duplicating error entries?

logger-debug.txt

Am I duplicating error entries?
Am I duplicating error entries?
Am I duplicating error entries?

third time I run the script:

console:

runfile('C:/Users/mds/Dropbox/_python/logger-debugger.py', wdir='C:/Users/mds/Dropbox/_python')
ERROR:__main__:Am I duplicating error entries?

logger-debug.txt

Am I duplicating error entries?
Am I duplicating error entries?
Am I duplicating error entries?
Am I duplicating error entries?
Am I duplicating error entries?
Am I duplicating error entries?
Alastair McCormack
  • 26,573
  • 8
  • 77
  • 100
user3556757
  • 3,469
  • 4
  • 30
  • 70

4 Answers4

11

apparently merely closing the handler is not enough. It also needs to be removed from the logger instance.

So now the final correct example is:

import logging

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)

"""remember to close this handler at finish!!!"""
hdlr = logging.FileHandler("/Users/mds/Dropbox/_python/logger-debug.txt")
logger.addHandler(hdlr) 


logger.error("Am I duplicating error entries?")

hdlr.close()
logger.removeHandler(hdlr)

Notice the logger.removeHandler(hdlr) in the new version.

So this cured the problem of locked files. It also cured the problem of multiple runs of the script writing the same log messages several times. Now it's apparent that was happening because there were multiple filehandlers still linked to that logger instance (main), so many handlers simultaneously wrote the same error message. This means I do not have to use overwrite filemode, I can use append filemode too.

I saw an old Stack Overflow message that had this bit of code for deleting all handlers.

handlers = logger.handlers[:]
for handler in handlers:
    handler.close()
    logger.removeHandler(handler)
Community
  • 1
  • 1
user3556757
  • 3,469
  • 4
  • 30
  • 70
2

Spyder IDE uses persistent instances of Python that you may run scripts in and interact with. Each time you run the script, the same instance is used - No objects are cleared.

The log duplication is caused because you're effectively adding multiple handlers to the same logger.

The lock will be present until you close and remove all handlers, or you kill the Python instance/"tab".

Alastair McCormack
  • 26,573
  • 8
  • 77
  • 100
2

As Alastair says, by default spyder do not clean all variables when re-running a script, so that handlers will be added each time. To avoid this, for your case, you can simply check if the handlers list is empty before adding one:

if len(logger.handlers) == 0 :
   logger.addHandler(hdlr) 
Guillaume
  • 938
  • 1
  • 7
  • 10
0

For 2 you can call the Filehandler with mode='w' instead of althea default append mode.

rfkortekaas
  • 6,049
  • 2
  • 27
  • 34
  • Yeah, that eliminate the problem. Although if I wanted to append, instead of overwrite,, I still don't understand why each write seems to re-duplicate lines previously-written in addition to adding the new line. – user3556757 Jan 01 '16 at 11:07
  • And the file-lock is still a problem. Cannot delete the file until I close the IDE. – user3556757 Jan 01 '16 at 11:08
  • Can you use another file location. I have seen similar issues on windows with Dropbox – rfkortekaas Jan 01 '16 at 11:20
  • dropbox wasn't the issue. I had to remove the filehandlers from the logger, not just close them. Means I don't need to set filemode, either. – user3556757 Jan 01 '16 at 11:29