logging of strings with broken encoding

Stefan Behnel stefan_ml at behnel.de
Thu Jul 2 19:01:37 CEST 2009


Thomas Guettler wrote:
> My quick fix is this:
> 
> class MyFormatter(logging.Formatter):
>     def format(self, record):
>         msg=logging.Formatter.format(self, record)
>         if isinstance(msg, str):
>             msg=msg.decode('utf8', 'replace')
>         return msg
> 
> But I still think handling of non-ascii byte strings should be better.
> A broken logging message is better than none.

Erm, may I note that this is not a problem in the logging library but in
the code that uses it? How should the logging library know what you meant
by passing that byte string in the first place? And where is the difference
between accidentally passing a byte string and accidentally passing another
non-printable object? Handling this "better" may simply hide the bugs in
your code, I don't find that's any "better" at all.

Anyway, this has been fixed in Py3.

Stefan



More information about the Python-list mailing list