Issue 14024: logging.Formatter Cache Prevents Exception Format Overriding (original) (raw)

logging.Formatter.format() creates a cache called exc_text with a copy of the traceback text which it uses for all log handlers (I think). When this cache is set, format() does not call formatException to format the exception/traceback data.

Unfortunately, this means that one cannot override logging.Formatter.formatException() for a specific log Handler. For example, to create a stripped-down exception message for emailing to a non-technical audience, one might create the derived class NoStaceTraceFormatter, and attach an instance to the SMTPHandler using setFormatter():

class NoStackTraceFormatter(logging.Formatter): def formatException(self, exc_info): # Don't emit the stack trace return '\n'.join(traceback.format_exception_only(exc_info[0], exc_info[1])) # type and value only

At least when other handlers exist for the logger, the new formatException call will probably never be invoked. (This might depend on the order in which the handlers are called when an exception log message arrives: the first handler would define the exception text.)

One partial workaround is to override the logging.Formatter.format() method to defeat the cache, causing the overriding formatException to be called.

def format(self, record):
    record.exc_text = None # Defeat the common cache so formatException will be called.
    return logging.Formatter.format(self, record)

Unfortunately, this will create a new cached copy of the special-purpose exception text, possibly causing downstream handlers to emit the wrong message.

A possible solution would be to move the caching from logging.Formatter.format() to logging.Formatter.formatException, which would at least allow an individual handler to ignore the cache. (However, all handlers would share the cache, possibly leading to trouble.)

The cache should probably be eliminated, as the premise on which it is based, "it's constant anyway", isn't strictly true.

A number of points:

  1. exc_text is not just an implementation detail - it's in the docs. Thus, removing the cache altogether would be backwards-incompatible.

  2. The exc_text value is the only simple way of propagating the exception information across the wire, which is a common use case (e.g SocketHandler).

  3. This is not a "behavior" issue, as the behaviour is by design and documented.

  4. What's wrong with the following approach?

class NoStackTraceFormatter(logging.Formatter): def formatException(self, exc_info): # Don't emit the stack trace return '\n'.join(traceback.format_exception_only(exc_info[0], exc_info[1])) # type and value only

def format(self, record):
    saved_exc_text = record.exc_text # optional
    record.exc_text = None
    result = super(NoStackTraceFormatter, self).format(record)
    record.exc_text = saved_exc_text # or None, if you want
    return result

You can either save and restore the previous exc_text value, or set it to None after the parent class operation - this will cause it to be always recomputed by the next handler. This way, a handler which needs abbreviated information always gets it, but other handlers append the full stack trace.

I'm closing this as I believe my suggestion shows a way of subclassing and overriding which works. You can re-open if you think I've missed something.