By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,931 Members | 1,388 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,931 IT Pros & Developers. It's quick & easy.

Logging to a file and closing it again properly (logging module)

P: n/a

I have an application that is running in an endless loop processing an
incoming queue. Every run is supposed to write a log file about the run
and then close it again. While the 'logging' module is generally working
well (even though the documentation makes me miss some proper examples
how everything works together) I can't seem to close the log file again
to open a new one.

This is basically what I'm doing:

log = logging.getLogger("myapplication")

Now I'm missing a way to tell this handler to go away. Through 'ipython'
I found out there is a log.handlers array that contains all the
handlers. Perhaps I could delete all of them but I'm sure there is a
more proper way to close files again.

Googling found me:

.>>> logging._handlers.clear()
.>>> logging.root.handlers = []
.>>> for l in logging.Logger.manager.loggerDict.values():
.>>> l.handlers = []

But this looks really really ugly. I don't like to mess with the gears
in the 'logging' module.


Jun 12 '06 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.