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
| Level | Use for |
|---|---|
| DEBUG | Details for fixing problems |
| INFO | Normal progress |
| WARNING | Something odd, still running |
| ERROR | Something failed |
| CRITICAL | The program can't continue |
In code
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
Summary
- Use logging, not print, for real programs.
- Use argparse to make a script configurable.
Finished reading?
Sources: Python docs: Logging HOWTO