logging of strings with broken encoding

Thomas Guettler hv at tbz-pariv.de
Fri Jul 3 09:02:06 CEST 2009


Stefan Behnel schrieb:
> 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?

I know that my code passes the broken string to the logging module. But maybe
I get the non-ascii byte string from a third party (psycopg2 sometime passes
latin1 byte strings from postgres in error messages).

I like Python very much because "it refused to guess". But in this case, "best effort"
is a better approach.

It worked in 2.5 and will in py3k. I think it is a bug, that it does not in 2.6.

 Thomas



-- 
Thomas Guettler, http://www.thomas-guettler.de/
E-Mail: guettli (*) thomas-guettler + de



More information about the Python-list mailing list