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.
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
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.
| Module | Role in the described setup |
|---|---|
| os | Interacting with the operating system |
| platform | Providing information about the platform, meaning the operating system |
| logging | Logging 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.
- 11. Identify the message
The program has an important message that should be retained rather than considered only during the program's current run.
- 22. Use logging
The logging module is the part of the described setup used to log information and provide a way to store the message.
- 33. Configure the destination
Configure the logging module to write messages to a file you have specified.
- 44. Configure the format
Configure the logging module so the messages are written in a particular format.
- 55. 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.
Common mistake Assuming that logging is configured without specifying a destination.
What it looks likeA learner expects messages to be available in a file but has not identified the file to which the logging module should write.
Why it failsThe described process includes configuring the logging module to write to a specified file.
Do this insteadInclude a specified file as part of the logging configuration.
Common mistake Ignoring the message format.
What it looks likeA learner focuses only on where messages are stored and overlooks the instruction to write them in a particular format.
Why it failsThe source identifies the format as part of how the logging module is configured.
Do this insteadTreat 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 likeA learner assigns the task of logging information to the module described as handling operating-system interaction or platform information.
Why it failsThe three modules have different roles in the described setup.
Do this insteadUse os for interacting with the operating system, platform for platform information, and logging for logging information.
Check Your Understanding
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
- 1The logging module can be used to store debugging or important messages for later inspection.
- 2The logging module is the component used to log information in the described setup.
- 3The os module is used for interacting with the operating system, while the platform module provides information about the operating system.
- 4Logging is configured with a particular message format and a specified file.
- 5The 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.