Long story short:
# this works as expected:
logging.error(my_object.__unicode__())
# this doesn't:
logging.error(my_object)
Same result with __str__(self)
.
Why?
EDIT: __str__
actually works.
Long story short:
# this works as expected:
logging.error(my_object.__unicode__())
# this doesn't:
logging.error(my_object)
Same result with __str__(self)
.
Why?
EDIT: __str__
actually works.
>>> class MyClass(object):
... def __str__(self):
... return "foobar"
...
>>> import logging
>>> logging.error(MyClass())
ERROR:root:foobar
Works as expected?!
Logging will call str()
(which uses __str__()
which falls back to __repr__()
when it's not defined). This is because the logging framework is not Unicode safe (i.e. you can get errors when you try to write Unicode to the various logging sinks).