Python Debugging Cheat Sheet/Use a readable log format

Add timestamps and levels to logs.

Section: Logging and Diagnostics

Use a readable log format

python
python
import logging

logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s %(levelname)s %(name)s %(message)s',
)

Learn the surrounding workflow

Compare similar commands or jump into common fixes when this command is part of a bigger troubleshooting path.

Related commands

Same sheet · prioritizing Logging and Diagnostics
Enable basic debug logging
Turn on debug-level logs quickly.
OpenIn sheetpythonsame section
Log an exception with traceback
Emit the stack trace automatically in an except block.
OpenIn sheetpythonsame section
Attach request or job context to logs
Add contextual identifiers that make debugging easier.
OpenIn sheetpythonsame section
Time a block of code
Measure elapsed time around suspicious code paths.
OpenIn sheetpythonsame section
Benchmark small snippets with timeit
Measure a small expression from the command line.
OpenIn sheetbashsame section
Format exception as a string
Capture traceback output for logs or APIs.
OpenIn sheetpython1 tag match