7

I'm using Python 2.7.9. x32 on Win7 x64.

When I'm logging an Exception containing Umlauts, I always receive
UnicodeDecodeError: 'ascii' codec can't decode byte 0xfc in position 39: ordinal not in range(128)

My example code is:

except Exception as e:
            logging.error('Error loading SCMTool for repository '
                          '%s (ID %d): %s' % (repo.name, repo.id, e),
                          exc_info=1)

The Exception being logged is WindowsError: [Error 267] Der Verzeichnisname ist ungültig. The Problem is based on the "ungÜltig" umlaut.

After removing the last %s and the e it works without a problem.

This happens everytime an exception is logged, therefore changing every logger is no alternative.

Does anyone have an idea how to make Exception return a unicode string globally?

1 Answer 1

15

You are trying to interpolate a unicode object into a str template, triggering an implicit encoding.

Use a unicode template; logging can handle Unicode just fine:

logging.error(u'Error loading SCMTool for repository '
              '%s (ID %d): %s' % (repo.name, repo.id, e),
              exc_info=1)

Two additional tips:

  • You don't have to do the interpolation yourself; if you pass in the 3 elements to interpolate as separate arguments, logging will interpolate for you, but only if the message is actually going to be emitted.

  • If you use logging.exception() the message is logged at the ERROR level and exc_info is set for you; it gets you the same result but is more easily recognised when reading your code later. Either way, the exception is already included in that case, no need to include it again in the message.

As such, I'd use:

logging.exception(
    'Error loading SCMTool for repository %s (ID %d)',
    repo.name, repo.id)
Sign up to request clarification or add additional context in comments.

11 Comments

This is not really my code, I'm using Reviewboard which contains a lot files with multiple Exception loggings. Is there no more central way? Where I do not have to touch all files.
Perhaps you should file a bug with the Reviewboard project then?
There is no more 'central' way, this is a bug with how exceptions are being handled.
I found the root cause. On one maschine I'M using Win7 english (here it works) And on the other german (Doesn't work). With your fix, the Exception WindowsError: [Error 267] Der Verzeichnisname ist ungültig' is logged. This contains an Ü which cannot be encoded to ascii. Thanks for your help!
@Seega: ah, I see what you mean, I misunderstood. Glad it is now working! :-D
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.