Concepts / Introduction to the logging Module

Introduction to the logging Module

The central purpose introduced here is simple: use the logging module to store debugging or important messages so they can be inspected later.

  • Programming

Messages Worth Keeping

When a program runs, you may want to keep debugging messages or important messages so you can check later whether the program has been running as expected. The logging module provides a way to store these messages somewhere instead of relying only on what is visible while the program is running.

Key point

The central purpose introduced here is simple: use the logging module to store debugging or important messages so they can be inspected later.

From Program to Stored Message

Running programproduces a messagelogging modulestores messagesConfigured formatmessage formatSpecified filestored messagesLater inspectioncheck program behaviormessageconfigurewriteinspect later
How does a debugging or important message travel from a running program to a place where it can be inspected later?

The message begins with the running program. The logging module provides the mechanism for storing it. Before messages are written, the logging module is configured with a particular format and with the file where the messages should be written. The resulting file gives you a place to inspect the messages after the program has been running.

The Three Standard Library Roles

The described setup uses three modules from Python's standard library, with each module serving a different role. The os module is used for interacting with the operating system. The platform module provides information about the platform, meaning the operating system. The logging module is used to log information.

ModuleRole in the described setup
osInteracting with the operating system
platformProviding information about the platform, meaning the operating system
loggingLogging information

The source describes these three standard-library modules as having distinct responsibilities.

A Message Through the Configuration

Worked example

Storing an Important Message

A running program produces an important message, and you want to check that message later to evaluate whether the program has been running as expected.

  1. 1
    1. Identify the message

    The program has an important message that should be retained rather than considered only during the program's current run.

  2. 2
    2. Use logging

    The logging module is the part of the described setup used to log information and provide a way to store the message.

  3. 3
    3. Configure the destination

    Configure the logging module to write messages to a file you have specified.

  4. 4
    4. Configure the format

    Configure the logging module so the messages are written in a particular format.

  5. 5
    5. Inspect the stored message

    Once the message has been written to the specified file, that file provides a place to check the message later.

Result

The message has a configured format and destination, allowing it to be stored in the specified file for later inspection.

The important state change in this example is not that the message becomes a different kind of information. The change is that the message moves from being produced during program execution to being written in a chosen format to a specified file, where it can be checked later.

Configuration Before Inspection

The source emphasizes configuration: the logging module is configured to write all messages in a particular format to the file that has been specified. This means the destination and the format are deliberate parts of the setup. If you want a useful stored record, decide where the messages should be written and how they should be represented before relying on the file for later checking.

  • Common mistake

    Assuming that logging is configured without specifying a destination.

    What it looks like

    A learner expects messages to be available in a file but has not identified the file to which the logging module should write.

    Why it fails

    The described process includes configuring the logging module to write to a specified file.

    Do this instead

    Include a specified file as part of the logging configuration.

  • Common mistake

    Ignoring the message format.

    What it looks like

    A learner focuses only on where messages are stored and overlooks the instruction to write them in a particular format.

    Why it fails

    The source identifies the format as part of how the logging module is configured.

    Do this instead

    Treat the message format and the destination file as two separate configuration concerns.

  • Common mistake

    Using os or platform as if either were the logging module.

    What it looks like

    A learner assigns the task of logging information to the module described as handling operating-system interaction or platform information.

    Why it fails

    The three modules have different roles in the described setup.

    Do this instead

    Use os for interacting with the operating system, platform for platform information, and logging for logging information.

Check Your Understanding

EASY

Describe the path of a debugging message from the moment a program produces it to the moment you inspect it later. In your answer, include the logging module, a particular message format, and a specified file.

Hints
  • Start with the running program and the message it produces.
  • Explain what the logging module does with the message.
  • Mention both parts of the configuration: the format and the file.

Key Takeaways

  1. 1
    The logging module can be used to store debugging or important messages for later inspection.
  2. 2
    The logging module is the component used to log information in the described setup.
  3. 3
    The os module is used for interacting with the operating system, while the platform module provides information about the operating system.
  4. 4
    Logging is configured with a particular message format and a specified file.
  5. 5
    The stored file gives you a place to check whether the program has been running as expected.

At a glance

  • The logging module provides a way to store debugging and important messages.
  • Stored messages can be inspected later to check how a program has been running.
  • The described setup separates the roles of os, platform, and logging.
  • The logging module is configured with a message format and a specified output file.

More concepts starting with I