← Back to articles

Python · Observability

Python Logging: Useful Context Without Duplicate Messages

Use named loggers, deliberate context and centralized handlers to make application logs easier to investigate.

Blue application modules send amber event signals through a single green lens into an organized log ledger.
Named event sources feed a shared logging destination.

Useful logs explain what happened and where it happened. Python’s logging module provides named loggers, severity levels and configurable output handlers.

Name the source
After importing logging, create logger = logging.getLogger(name) in each module. The name follows the package hierarchy, making noisy components easier to tune separately.

Pass values separately
Use logger.info("Job completed: %s", job_id) rather than assembling every message manually. Choose identifiers that help investigate an event without exposing passwords, tokens or full request bodies.

Keep exception context
Inside an exception handler, logger.exception("Import failed") includes exception information. Record a failure where useful context is available, and avoid repeating the same traceback at every layer.

Configure delivery centrally
Handlers decide where records go; formatters control their presentation. A common setup attaches handlers at the root and lets module loggers propagate. Attaching handlers to both a child and its ancestors can emit the same record repeatedly.

Check the actual configuration
A logger’s effective level and its handlers’ thresholds both affect output. Confirm that expected events arrive once, with a timestamp, severity and logger name, before relying on the logs during an incident.