How do you log an exception with its traceback?
BeginnerConceptExceptions, context managers, files & logging
Answer
Inside an except block, use log.exception("msg"), which logs at ERROR and appends the traceback. At other levels pass exc_info=True.
try:
client.delete_snapshot(SnapshotId=sid)
except ClientError:
log.exception("failed to delete %s", sid)
raise
log.error(str(e)) loses the stack, which is the one thing you need at 3 AM.
More on Exceptions, context managers, files & logging
How does `try` / `except` / `else` / `finally` work?BeginnerWhy is a bare `except:` (or `except Exception: pass`) dangerous?BeginnerWhat is exception chaining, and what is `raise ... from`?IntermediateWhen should you define custom exception classes?IntermediateWhat is the EAFP vs LBYL style, and which does Python prefer?IntermediateHow does a `with` statement work, and why use it for files?Beginner