by teddyh · 3y ago · 0 comments
In Python, exception handling is part of the normal control flow. Therefore, when an exception happens, it is not always the case that you want to log a full stack trace.
try:
thing = things[0]
except IndexError:
log.error("Not enough things!")
return
I think that even recommending a log.exception() here would actually be a bad thing.But it can be helpful to log an error message (without stacktrace) to give context that is available only in this frame before letting the exception propagate to whoever ultimately handles and logs it.
If it’s expected, I tend not to log it.
This rule is only for error calls, though. In that case, yeah, you wouldn't log an error for an expected path.