liveBook · Manning

Monitoring

As explained in Python How-To

Logging is the recording of program events and diagnostic information through a structured message mechanism.

How will you investigate a failure that users encounter but you cannot reproduce? Record application events as they happen. Logs preserve clues about what the program did, making reported problems easier to diagnose. Add logging during development rather than waiting for a production failure. section 12.0 section 12.1

Keep an error record

import logging logger = logging.getLogger(__name__) file_handler = logging.FileHandler("taskier.log") logger.addHandler(file_handler) logger.error("Failed to save the task to the db") The file now contains: Failed to save the task to the db. Unlike console output from print, the record remains available after the console closes. Retrieving the logger by module name also avoids creating separate loggers for that module. section 12.1.2 section 12.2.3 section 12.6

Configure the logger to keep useful records without burying important ones:

  • Add a StreamHandler alongside the FileHandler when you also want real-time console feedback during development; both handlers can receive the same event. section 12.1.3
  • Choose among DEBUG, INFO, WARNING, ERROR, and CRITICAL by severity. A logger set to WARNING captures WARNING and more serious records, but not INFO or DEBUG. section 12.2.1
  • Set levels on individual handlers when destinations need different records. Keep the logger’s threshold low enough to admit anything those handlers should receive. section 12.2.2 section 12.2.4
  • Attach a Formatter to make each record easier to find and interpret. A timestamp, severity, module name, and message provide more context than a bare message. section 12.2.3
Listing 12.6 Setting levels to individual file handlers
logger.setLevel(logging.DEBUG)                  #1
 
handler_warning = logging.FileHandler("taskier_warning.log")
handler_warning.setLevel(logging.WARNING)       #2
logger.addHandler(handler_warning)
 
handler_critical = logging.FileHandler("taskier_critical.log")
handler_critical.setLevel(logging.CRITICAL)     #3
logger.addHandler(handler_critical)
 
logging_messages_all_levels()
 
warning_log_records = check_log_content("taskier_warning.log")
print(warning_log_records)
# output the following lines:
--Critical message
--Error message
--Warning message
 
critical_log_records = check_log_content("taskier_critical.log")
print(critical_log_records)
# output the following line:
--Critical message

Compare the two output files: the WARNING handler keeps warnings, errors, and critical messages, while the CRITICAL handler keeps only critical messages. section 12.2.2

For ongoing monitoring, save important events to a file; use console output as a convenient development aid. Format records and choose levels deliberately so a large log remains useful when a problem appears. section 12.1.4 section 12.2

How Python How-To covers it

How deep it goes
Works through logger retrieval, file and stream handlers, severity thresholds, and record formatting; mentions other handler types without detailing them.
Examples
Uses a task-management application, duplicate file destinations with different thresholds, and formatted console records.
What it assumes
Assumes basic Python method calls and file reading while introducing logger, handler, level, and formatter configuration.
What you can do after
Apply it via Python’s logging module to save, filter, and format events for later diagnosis.
How it connects
Connects logging to debugging, file I/O, application monitoring, and exception handling.

Also explained in

Compare how 40 books explain monitoring →

Related concepts

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