the full guide

logging and argparse

logging records what a program does, with levels such as DEBUG, INFO, WARNING and ERROR, and it's the proper replacement for leaving print statements around. argparse turns a script into a command-line tool with named options and automatic help.

10 min Intermediate

What you need: The earlier topics of this module.

Definition

logging records what a program does, with levels such as DEBUG, INFO, WARNING and ERROR, and it's the proper replacement for leaving print statements around. argparse turns a script into a command-line tool with named options and automatic help.

In simple words

print shouts everything to everyone. logging is a diary with importance levels: you decide how much to read, and where to save it. argparse is what lets someone run python train.py --epochs 10.

Why it matters

A training job that runs all night needs a log to read in the morning. Command-line options let you run many experiments without editing code.

How it works

LevelUse for
DEBUGDetails for fixing problems
INFONormal progress
WARNINGSomething odd, still running
ERRORSomething failed
CRITICALThe program can't continue

In code

cli_tool.py
import argparse
import logging
import sys

logging.basicConfig(stream=sys.stdout, level=logging.INFO, format="%(levelname)s %(message)s")
log = logging.getLogger("trainer")

parser = argparse.ArgumentParser(description="Train a model")
parser.add_argument("--epochs", type=int, default=3)
parser.add_argument("--fast", action="store_true")

# Normally this reads the real command line. Here we pass it directly.
args = parser.parse_args(["--epochs", "2", "--fast"])

log.debug("This is hidden at INFO level")
log.info("Starting with %d epochs", args.epochs)
if args.fast:
    log.warning("Fast mode can reduce accuracy")

Output

INFO Starting with 2 epochs
WARNING Fast mode can reduce accuracy

Common mistakes

  • Leaving print statements in production code, where they can't be filtered.
  • Logging passwords or personal data, because logs get stored and shared.

Check yourself

Question 1

1. Which level is hidden when the level is set to INFO?

Question 2

2. What does argparse provide?

Summary

  • Use logging, not print, for real programs.
  • Use argparse to make a script configurable.

Finished reading?

Sources: Python docs: Logging HOWTO