[ python-Bugs-1501699 ] method format of logging.Formatter caches incorrectly

SourceForge.net noreply at sourceforge.net
Fri Jun 23 16:01:34 CEST 2006


Bugs item #1501699, was opened at 2006-06-06 17:14
Message generated for change (Comment added) made by blorbeer
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1501699&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Python Library
Group: None
>Status: Open
Resolution: Invalid
Priority: 5
Submitted By: Boris Lorbeer (blorbeer)
Assigned to: Vinay Sajip (vsajip)
Summary: method format of logging.Formatter caches incorrectly

Initial Comment:
The format method of logging.Formatter is buggy in that
it doesn't call the method formatException if the cache
record.exc_text is set. If you have two Formatters that
should format the same log record differently (i.e. each
has its own overriding formatException method), the
formatException method of the second formatter will
never be called because the cache has been set by the
first formatter. The proper way of using the cache is
IMHO to check the cache only in the method
formatException of logging.Formatter.

----------------------------------------------------------------------

>Comment By: Boris Lorbeer (blorbeer)
Date: 2006-06-23 16:01

Message:
Logged In: YES 
user_id=1535177

Hi vsajip,
yes, it is by design, but I don't know whether the design is
ideal. But if this behaviour is really intended, it should
be documented clearly, such as:
formatException(exc_info): If you override this method, an
exception in the log record will be formatted by using this
method, but only if this log record wasn't given by the
framework to another formatter (that uses the default format
function) before your formatter got its turn (something you
cannot ensure)...

Now to the question of how to fix the design (provided one
wants to): clearly one cannot change the signature of
formatException without breaking existing code. But one
could change formatter to have an additional field
'labeledCache': a pair of an exc_info tuple and a string
(the cache). The formatException method would then use this
cache only if id() of its argument is the id() of the first
element in the pair, otherwise it would exchange
'labeledCache' by a new pair belonging to the current
exc_info tuple. That's only one posibility to fix this problem.

----------------------------------------------------------------------

Comment By: Vinay Sajip (vsajip)
Date: 2006-06-22 18:46

Message:
Logged In: YES 
user_id=308438

It's not a bug, it's by design. The formatException method
only takes the exception info as a parameter, and to change
the  method signature now could break some people's code, right?

A solution would be for you to also override the format
method in your custom formatter classes and set
record.exc_text to None if you want to invalidate the cache
before calling the base class implementation.

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1501699&group_id=5470


More information about the Python-bugs-list mailing list