# Logging & Debugging — Python Fundamentals: From Basics to Async

Source: https://www.geekswithgeeks.com/en/python/py-logging-debugging

> Replace print debugging with the logging module and step through code with pdb and breakpoint().

## The logging module

`logging` adds levels, timestamps and destinations, and can be turned up or down without editing code.

```python
import logging

logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s")
log = logging.getLogger(__name__)

log.info("Starting job")
log.warning("Disk at %d%%", 91)
try:
    1 / 0
except ZeroDivisionError:
    log.exception("Calculation failed")
```

## breakpoint() and pdb

`breakpoint()` drops you into the debugger at that line. Use `n` (next), `s` (step), `c` (continue) and `p expr` (print).

```python
def total(prices):
    result = 0
    for p in prices:
        breakpoint()   # inspect p and result here
        result += p
    return result
```

Use levels deliberately: DEBUG for detail, INFO for milestones, WARNING for odd events, ERROR for failures. Never log passwords or tokens.
