typestar

logging in Python

A logger per module, a level, and a format: never print for diagnostics.

import logging

logging.basicConfig(
    level=logging.INFO,
    format="%(levelname)s %(name)s: %(message)s",
)
log = logging.getLogger("typestar.ingest")

log.debug("not shown at INFO")
log.info("loaded %d seeds", 143)
log.warning("snippet too long: %s", "big.py")

try:
    1 / 0
except ZeroDivisionError:
    log.exception("while scoring")

How it works

  1. getLogger(__name__) names the logger after its module.
  2. The level filters; the handler decides where it goes.
  3. exception logs the traceback inside an except block.

Keywords and builtins used here

The run, in numbers

Lines
16
Characters to type
327
Tokens
81
Three-star pace
105 tpm

At the three-star pace of 105 tokens a minute, this run takes about 46 seconds.

Type this snippet

Step 3 of 4 in Command line, step 24 of 41 in Domain tools.

← Previous Next →