Logging exceptions to a file
Pierre GM
pierregmcode at gmail.com
Wed May 6 23:51:35 EDT 2009
All,
I need to log messages to both the console and a given file. I use the
following code (on Python 2.5)
>>> import logging
>>> #
>>> logging.basicConfig(level=logging.DEBUG,)
>>> logfile = logging.FileHandler('log.log')
>>> logfile.setLevel(level=logging.INFO)
>>> logging.getLogger('').addHandler(logfile)
>>> #
>>> mylogger = logging.getLogger('mylogger')
>>> #
>>> mylogger.info("an info message")
So far so good, but I'd like to record (possibly unhandled) exceptions
in the logfile.
* Do I need to explicitly trap every single exception ?
* In that case, won't I get 2 log messages on the console (as
illustrated in the code below:
>>> try:
>>> 1/0
>>> except ZeroDivisionError:
>>> mylogger.exception(":(")
>>> raise
Any comments/idea welcomed
Cheers.
More information about the Python-list
mailing list