SyntaxStudy
Sign Up
Python Intermediate 3 min read

Logging Exceptions

Logging Exceptions

Use the logging module with logging.exception() to record errors with full tracebacks in production code.

Example
import logging

logging.basicConfig(level=logging.ERROR)

try:
    result = 1 / 0
except ZeroDivisionError:
    logging.exception("Unexpected error occurred")
Pro Tip

logging.exception() automatically includes the traceback.