Repeated output when logging exceptions

Vinay Sajip vinay_sajip at yahoo.co.uk
Fri Sep 25 16:23:04 EDT 2009


On Sep 24, 8:43 pm, John Gordon <gor... at panix.com> wrote:
> Why is this happening?  I suspect it's because I'm declaring two instances
> of the exceptionLogger class, which ends up calling logger.addHandler()
> twice.  Is that right?
>

Yes, that's why you get duplicated lines in the log.

> What would be a better way to handle this?

I'm not sure why you need all the code you've posted. The logging
package allows you to add tracebacks to your logs by using the
exception() method, which logs an ERROR with a traceback and is
specifically intended for use from within exception handlers. All that
stuff with temporary files seems completely unnecessary, even with
Python 2.3.

In general, avoid adding handlers more than once - which you are
almost guaranteed to not avoid if you do this kind of processing in a
constructor. If you write your applications without using the
exceptionLogger class (not really needed, since logging exceptions
with tracebacks is part and parcel of the logging package's
functionality since its introduction with Python 2.3), what
functionality do you lose?

Regards,

Vinay Sajip



More information about the Python-list mailing list