tags:

views:

38

answers:

2

I have a logging component in my program. The setup of the formatter is straightforward:

sh.setFormatter(logging.Formatter("%(asctime)s - %(message)s"))

I notice that my program is having problems. After a certain point, the formatter reverts to the default configuration (i.e., ignores the formatting I supplied). On closer inspection it seems that I am crashing it by sending a message that throws a UnicodeDecodeError when rendered in the string. But, I can't seem to fix.

I wrapped the logging call:

try:
    my_logger.info(msg)
except UnicodeDecodeError:
    pass

Which "catches" the exception, but the logger is still pooched.

Any thoughts?

+1  A: 

Any idea what input is causing the UnicodeDecodeError? Ample printing of variables would help! If you want to move on upon receiving that error, you should wrap the calls to the formatter in a try..except block.

try:
    # log stuff
except UnicodeDecodeError:
    # handle the exception and move on

It would be helpful to see some more code and some of your input data to give you a more clear response.

jathanism
I did that wrap exactly. The error is caught, but the logger is still botched.
muckabout
Perhaps a simple re-initialization of your formatter upon catching an exception? It's hard to tell without seeing the exact cause of the problem.
jathanism
+1  A: 

Take a look at this: http://wiki.python.org/moin/UnicodeDecodeError. You probably have some string that can't be decoded.

Maciej Kucharz