我目前正在编写一个包装类。我希望能够正确记录异常,但允许调用方法知道发生的异常。我的阶级是这样的:Python日志记录异常
import logging
log = logging.getLogger('module')
class MyAPIWrapper(library.APIClass):
def __init__(self):
self.log = logging.getLogger('module.myapiwrapper')
def my_wrapper_method(self):
try:
response = self.call_api_method()
return response.someData
except APIException, e:
self.log.exception('Oh noes!')
raise e #Throw exception again so calling code knows it happened
我有点半信半疑捕获和异常只是为了记录它,然后重新认识它,以便调用代码可以做什么。这里有什么合适的模式?
[蟒异常记录](HTTP的可能重复:// stackoverflow.com/questions/5191830/python-exception-logging) – lpapp
这正是我正在做的。感谢您发布这个问题。 – smwikipedia