1

I'm trying to log info from multiple modules that I've created. I have a main script (main.py) and another module (let's call it mymodule.py). I've set up a logger in each module as follows:

logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)

# create a file handler
file_handler = logging.FileHandler('debug.log', mode='a')
file_handler.setLevel(logging.DEBUG)

# create a logging format
# we're limiting the levelname to 1 char: I=INFO, D=DEBUG and so on
formatter = logging.Formatter('[%(levelname).1s][%(module)14s:%(funcName)-15s]-> %(message)s')
file_handler.setFormatter(formatter)

# add the handler to the logger
logger.addHandler(file_handler)

In main.py I load mymodule.py and I do the following at the end of all processing.

file_handler.close()
logger.removeHandler(file_handler)

Now the problem is that once the script stops running I can't delete debug.log unless I close Spyder. Also, for each subsequent run I get duplicate entries from mymodule.py like this.

[D][mymodule:__init__       ]-> object created
[D][mymodule:__init__       ]-> object created

So it appears that there is an open file handler that I've left hanging. How do I close it. If I have to add a file_handler.close() in mymodule.py where do I add it?

EDIT: I tried this in main.py

# clean up file handlers
handlers = logger.handlers[:]
for handler in handlers:
    handler.close()
    logger.removeHandler(handler)
Paulie-C
  • 1,674
  • 1
  • 13
  • 29
kronosjt
  • 703
  • 4
  • 10
  • 24
  • Have you checked `logger.handlers` to see that you have indeed removed all handlers at the end? –  May 02 '17 at 05:35
  • I tried updating main.py. Please see the edited question. That didn't help. – kronosjt May 02 '17 at 06:50
  • Use `logging` like described here: https://docs.python.org/3.6/howto/logging.html#logging-from-multiple-modules – stovfl May 02 '17 at 09:58
  • I could use that method if I wasn't using a custom logger but I'd like to use a custom logger. – kronosjt May 02 '17 at 12:52

1 Answers1

0

If you are using Spyder, then your variables are probably alive for the entire lifetime of the workspace. You should just delete the variable file_handler:

file_handler.close()
del file_handler
Liran Funaro
  • 2,750
  • 2
  • 22
  • 33