Concepts / Introduction to the logging Module

Introduction to the logging Module

What if you wanted to have some debugging messages or important messages to be stored somewhere so that you can check whether your program has been running as you would expect it? How do you "store somewhere" these messages? This can be achieved using the logging module.

  • 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.

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

messageconfigurewriteinspect laterRunning programproduces a messagelogging modulestores messagesConfigured formatmessage formatSpecified filestored messagesLater inspectioncheck program behavior
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

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. Identify the message: The program has an important message that should be retained rather than considered only during the program's current run.

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. Configure the destination: Configure the logging module to write messages to a file you have specified.

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

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.

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.

  • Assuming that logging is configured without specifying a destination.

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

    Fix: Include a specified file as part of the logging configuration.

  • Ignoring the message format.

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

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

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

    The three modules have different roles in the described setup.

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

Key Takeaways

  • 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.