liveBook · Manning

Logging module

As explained in The Art of Code

Python's logging module is a standard-library facility for creating, filtering, formatting, and routing log records.

The logging module replaces fragile console output with a controlled way to route useful records toward persistent, aggregated, or monitored destinations. It supports filtering, formatting, destinations, and severity levels, making diagnostic information more useful across development and production environments. section 7.2.3

Good logging communicates what happened and where. Messages such as “An error occurred” or “Execution failed” add little investigative value; include the relevant operation, input, and exception instead. section 7.2.3

try {
  int parsedNumber = Integer.parseInt(data);
} catch (NumberFormatException exception) {
  exception.printStackTrace();                #1
  System.out.println("Big problem!");    #2
  System.err.println("Big problem!");    #3
}

Console printing and stack traces expose failures only at the terminal, limiting persistence, aggregation, and monitoring. section 7.2.3

logger.error("Error while parsing date '{}'", rawDate, exception);

A useful error message names the failed operation, preserves the relevant value, and records the exception for investigation. section 7.2.3

How The Art of Code covers it

How deep it goes
Explains destinations, formats, filtering, severity levels, and message quality, but does not detail Python configuration APIs.
Examples
Uses console printing, stack traces, vague messages, and contextual parsing errors to contrast weak and useful diagnostics.
What it assumes
Assumes familiarity with exceptions and distinguishes development console output from server-side deployment needs.
What you can do after
Evaluate/understand when console output is insufficient and how contextual messages improve failure investigation.
How it connects
Connects logging with exception handling, console output, log levels, log files, and centralized logging systems.

Also explained in

Compare how 4 books explain logging module →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Art of Code. The section links point to the parts of the book it draws on.