Log exception with traceback in Python
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
When an exception occurs in a Python application, the traceback — the chain of function calls leading to the error — is essential for debugging. Simply catching and printing the exception message loses this context. Python's logging module provides logger.exception() and exc_info=True to capture the full traceback in log output, making it possible to diagnose errors from log files without a debugger attached.
Method 1: logging.exception()
The simplest and most common approach — call logger.exception() inside an except block:
logger.exception() automatically logs at ERROR level and includes the full traceback. It must be called inside an except block where the exception is active.
Method 2: exc_info=True
Use exc_info=True with any log level — not just ERROR:
This is useful when you handle the error gracefully but still want the traceback logged at a non-ERROR level.
Method 3: traceback Module
For more control over traceback formatting, use the traceback module directly:
Extract Structured Traceback Info
Configuring Log Format for Exceptions
Logging Exceptions in Async Code
Logging with Context (Extra Fields)
Add structured context to exception logs:
Anti-Pattern: Swallowing Exceptions
Common Pitfalls
- Using
logger.error(str(e))instead oflogger.exception(): This logs only the exception message, losing the traceback. Always uselogger.exception()orexc_info=Trueto capture the full stack trace. - Calling
logger.exception()outside an except block: It will logNoneType: Noneas the traceback because there is no active exception. Only call it inside anexceptclause. - Catching too broadly:
except Exceptioncatches everything including programming errors likeTypeErrorandAttributeError. Catch specific exceptions when possible, and useexcept Exceptiononly as a top-level safety net. - Logging and re-raising without care: If you
logger.exception()and thenraise, the same exception may be logged multiple times as it propagates up through multiple try/except blocks. Log at the point where you handle the error, not at every level. - Missing logger configuration: If
logging.basicConfig()is never called and no handlers are configured, log messages are silently discarded (Python 3.2+ shows a warning). Always configure logging at application startup.
Summary
- Use
logger.exception("message")insideexceptblocks to log the full traceback at ERROR level - Use
logger.error("message", exc_info=True)or any other level withexc_info=Truefor non-ERROR traceback logging - Use the
tracebackmodule (traceback.format_exc()) when you need the traceback as a string - Never log just
str(e)— the traceback is the most valuable debugging information - Configure logging with file handlers and formatters for production applications
Related reading
- log to specific logstream in cloudwatch from lambda
- Log whether a global variable has been read or written
- Logback JsonLayout printing all logs on the same line
- Logback to log different messages to two files
- logger configuration to log to file and print to stdout
- Logging all queries with cassandra-python-driver
- LogCat message The Google Play services resources were not found. Check your project configuration to ensure that the resources are included
- Logging uncaught exceptions in Python

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.