긴 이야기를 기록에 의해 호출되지 않습니다 __str__(self)
와__unicode의 __ (자기가) 짧은
# this works as expected:
logging.error(my_object.__unicode__())
# this doesn't:
logging.error(my_object)
같은 결과. 왜?
편집 : __str__
실제로 작동합니다.
긴 이야기를 기록에 의해 호출되지 않습니다 __str__(self)
와__unicode의 __ (자기가) 짧은
# this works as expected:
logging.error(my_object.__unicode__())
# this doesn't:
logging.error(my_object)
같은 결과. 왜?
편집 : __str__
실제로 작동합니다.
로깅은 str()
(__str__()
을 사용하며 정의되지 않은 경우 __repr__()
으로 되돌아갑니다). 이는 로깅 프레임 워크가 유니 코드에 안전하지 않기 때문입니다. 즉, 다양한 로깅 싱크에 유니 코드를 작성하려고하면 오류가 발생할 수 있습니다.
>>> class MyClass(object):
... def __str__(self):
... return "foobar"
...
>>> import logging
>>> logging.error(MyClass())
ERROR:root:foobar
예상대로 작동합니까?!
실제로 밝혀졌습니다. – yanchenko
사실이 아닙니다. 로깅은'str (msg) % args'를 사용하여 메시지를 형식화합니다. Python 2.x에서 stringify-behavior를 제공하려면'__str __()'과'__unicode__'를 항상 구현해야합니다. –