# Log Levels and What to Log — Logging, Metrics, Tracing & Alerting

Source: https://www.skillbyai.com/en/observability/ob-log-levels

> Pick the right log level for an event, configure a threshold, and avoid both noisy and useless logs.

## Levels are a contract with the reader

A **log level** says how important an event is. The common ladder is `DEBUG` < `INFO` < `WARNING` < `ERROR` < `CRITICAL` (some libraries also have `TRACE` and `FATAL`). The logger is configured with a **threshold**; anything below it is discarded cheaply, before formatting.

- **DEBUG**: internal detail useful while developing. Usually off in production.
- **INFO**: normal, meaningful business events: server started, order placed, job finished.
- **WARNING**: something unexpected that the system handled: a retry, a fallback, a deprecated call.
- **ERROR**: an operation failed and a user or job was affected.
- **CRITICAL**: the process cannot continue.

The rule of thumb: an `ERROR` should mean someone may need to act. If your logs have thousands of errors a minute that nobody reads, the level has lost its meaning and real failures hide in the noise. Log at the boundary where you *handle* an exception, not at every layer it passes through.

## Python's logging module with a threshold

The logger's level is `INFO`, so the `DEBUG` call is skipped. Note the `%s` placeholders: arguments are formatted only if the record is actually emitted.

```python
import logging, sys

logging.basicConfig(
    level=logging.INFO,
    format="%(levelname)-7s %(name)s: %(message)s",
    stream=sys.stdout,
)
log = logging.getLogger("checkout")

log.debug("cart=%s", ["pen", "book"])        # below INFO: dropped
log.info("order placed id=%s total=%d", "A17", 499)
log.warning("payment retry attempt=%d", 2)
log.error("payment failed gateway=%s", "upi")
```

Output:

```
INFO    checkout: order placed id=A17 total=499
WARNING checkout: payment retry attempt=2
ERROR   checkout: payment failed gateway=upi
```

## Use lazy formatting, not f-strings, in hot paths

`log.debug(f"cart={cart}")` builds the string even when DEBUG is off. `log.debug("cart=%s", cart)` defers the work until the record passes the level check. The difference is small per call but adds up in tight loops. Also never log the same exception at three layers; you get three stack traces for one failure.

**Quiz:** A payment gateway call failed, the code retried once and the retry succeeded. What level fits the first failure best?

- [ ] CRITICAL
- [ ] ERROR
- [ ] DEBUG
- [x] WARNING

*Answer:* WARNING. The system handled the problem and the user was not affected, which is exactly what WARNING is for. ERROR should be reserved for failures that affected an operation.
