Issue 1295: logging records cache the result of formatException() (original) (raw)

Created on 2007-10-18 15:30 by theller, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
logging.patch theller,2007-10-18 15:30
Messages (6)
msg56525 - (view) Author: Thomas Heller (theller) * (Python committer) Date: 2007-10-18 15:30
I needed two logging handlers in my application, one notifiying the user of errors, the other writing errors to a logfile. So I created a custom subclass of logging.Formatter and redefined the formatException() method that returned a summary of the exception like 'ZeroDivisionError' instead of the full traceback. Unfortunately the logging record caches the result of the handler.formatException() call in the exc_text variable, and the formatException() method of the second handler isn't called at all. The attached patch removes the caching and fixes the problem.
msg56530 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2007-10-18 17:01
This is tough. On the one hand you are right that different classes that have different formatException() methods aren't treated correctly; on the other hand I think the caching is important for other cases where there are multiple loggers all using the default formatException(). I recommend that instead of getting rid of the cache, you fix your class by overriding format() to reset record.ex c_text and then calling the base class format() method.
msg56536 - (view) Author: Thomas Heller (theller) * (Python committer) Date: 2007-10-18 17:49
> > This is tough. On the one hand you are right that different classes that > > have different formatException() methods aren't treated correctly; on > > the other hand I think the caching is important for other cases where > > there are multiple loggers all using the default formatException(). > > > > I recommend that instead of getting rid of the cache, you fix your class > > by overriding format() to reset record.ex c_text and then calling the > > base class format() method. That is what I have been doing although I think that I have to reset record.exc_text also AFTER the call to format() for the following handlers, if any. So, should this issue be closed as 'wont fix' ?
msg56537 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2007-10-18 17:53
I think so. On 10/18/07, Thomas Heller <report@bugs.python.org> wrote: > > Thomas Heller added the comment: > > > > This is tough. On the one hand you are right that different classes that > > > have different formatException() methods aren't treated correctly; on > > > the other hand I think the caching is important for other cases where > > > there are multiple loggers all using the default formatException(). > > > > > > I recommend that instead of getting rid of the cache, you fix your class > > > by overriding format() to reset record.ex c_text and then calling the > > > base class format() method. > > That is what I have been doing although I think that I have to reset > record.exc_text also AFTER the call to format() for the following > handlers, if any. > > So, should this issue be closed as 'wont fix' ? > > __________________________________ > Tracker <report@bugs.python.org> > <http://bugs.python.org/issue1295> > __________________________________ >
msg56538 - (view) Author: Thomas Heller (theller) * (Python committer) Date: 2007-10-18 18:03
I think that a warning or an example in the docs would be nice, but I have no time to make a patch for that.
msg61430 - (view) Author: Vinay Sajip (vinay.sajip) * (Python committer) Date: 2008-01-21 17:55
I've updated the docs (Formatter.format) to mention the caching and its effect on multiple formatters.
History
Date User Action Args
2022-04-11 14:56:27 admin set github: 45636
2008-01-21 17:55:15 vinay.sajip set nosy: + vinay.sajipmessages: +
2007-10-18 18:03:20 theller set status: open -> closedresolution: wont fixmessages: +
2007-10-18 17:53:25 gvanrossum set messages: +
2007-10-18 17:49:35 theller set messages: +
2007-10-18 17:01:37 gvanrossum set nosy: + gvanrossummessages: +
2007-10-18 15:30:36 theller create