This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: logging records cache the result of formatException()
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.0, Python 2.4, Python 2.6, Python 2.5
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: Nosy List: gvanrossum, theller, vinay.sajip
Priority: normal Keywords: patch

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:27adminsetgithub: 45636
2008-01-21 17:55:15vinay.sajipsetnosy: + vinay.sajip
messages: + msg61430
2007-10-18 18:03:20thellersetstatus: open -> closed
resolution: wont fix
messages: + msg56538
2007-10-18 17:53:25gvanrossumsetmessages: + msg56537
2007-10-18 17:49:35thellersetmessages: + msg56536
2007-10-18 17:01:37gvanrossumsetnosy: + gvanrossum
messages: + msg56530
2007-10-18 15:30:36thellercreate