The os Module for System Interaction
The logging module allows you to store debugging, information, warning, and critical messages to a file for post-execution program verification.
When the Program Says Nothing
A program can complete its work without displaying information directly to the user. Logging provides another way to preserve what happened: the program records debugging, information, warning, and critical messages in a file. After execution, a developer can inspect that file to verify the program's activity.
The Logging Route
The source describes logging as a route rather than simply a message-writing action. Log messages flow through Loggers, Handlers, and Formatters before reaching their final destination. In this article's focus, that destination is a file. The Logger is part of the message path, the Handler manages the destination path, and the Formatter determines how the message is represented before it reaches the file.
Logging is the use of the logging module to store debugging, information, warning, and critical messages in a file so that a program can be verified after it has finished executing.
Configuring the Record
Logging is configured with logging.basicConfig(). The configuration identifies four important choices: the severity level, the message format, the filename, and the file mode. These choices determine what kind of logging setup the program uses, how each stored message is represented, which file receives the messages, and how the file is opened.
A Configuration Plan
A program must preserve messages for later verification in a file.
- 1Choose a severity level
Decide which severity level the logging setup should use. The available message categories described in the source are debugging, information, warning, and critical.
- 2Choose a message format
Specify how each stored message should be represented in the log.
- 3Choose a filename
Identify the file that will receive the messages.
- 4Choose a file mode
Decide how the file should be opened as part of the logging configuration.
- 5Inspect the result
After execution, inspect the file to verify what the program recorded.
A complete logging plan connects message severity, message representation, file destination, and file mode to post-execution verification.
Portable File Destinations
A log file still needs a valid path. The source recommends using os.path.join() with platform detection to construct paths that work correctly on both Windows and Unix-based systems. An environment-dependent directory and a filename can therefore be treated as separate path parts before they are combined into the destination used by logging.
Keep the environment-dependent directory and the log filename conceptually separate until the path is constructed. Use os.path.join() together with platform detection rather than treating a path as a fixed form that must be identical on every operating system.
Verifying a Finished Run
Logging becomes especially useful after the program has stopped. During execution, messages are sent through the logging route and stored in the configured file. After execution, the developer can inspect that file and use its contents to verify what the program recorded, even when no output was displayed to the user.
Mistakes in Logging Design
Common mistake Treating logging as only a way to display a message while the program is running.
What it looks likeThe program shows no output, so the developer assumes there is no record of its activity.
Why it failsThe logging module can store debugging, information, warning, and critical messages in a file for post-execution verification.
Do this insteadThink of logging as a persistent record that can be inspected after execution.
Common mistake Using a fixed path without considering the operating system.
What it looks likeThe same unadapted path representation is used for every environment.
Why it failsThe source recommends
os.path.join()with platform detection so paths work correctly on Windows and Unix-based systems.Do this insteadBuild the path from an environment-dependent directory and filename using os.path.join().
Common mistake Configuring logging without considering all four configuration choices.
What it looks likeOnly a severity level is selected while the message format, filename, or file mode is ignored.
Why it failslogging.basicConfig()is used to specify the severity level, message format, filename, and file mode.Do this insteadReview all four choices when designing the logging configuration.
Common mistake Ignoring the components between a message and its destination.
What it looks likeThe developer treats the message as if it moves directly from the program to the file.
Why it failsLog messages flow through Loggers, Handlers, and Formatters before reaching their final destination.
Do this insteadTrace the message through the Logger, Handler, and Formatter when reasoning about the logging route.
Apply the Design
Design a logging plan for a program that produces no visible output. Identify the message categories you want to store, the severity level, the message format, the filename, and the file mode. Then describe how you would construct the filename from an environment-dependent directory so that the path works on Windows and Unix-based systems. Finally, explain how inspecting the file after execution would help verify the run.
Hints
- Use the four configuration choices associated with
logging.basicConfig(). - Include debugging, information, warning, and critical messages in your consideration of message categories.
- Use
os.path.join()with platform detection for the directory and filename. - End by describing post-execution inspection of the log file.
Key Takeaways
- 1The logging module stores debugging, information, warning, and critical messages in a file for post-execution program verification.
- 2
logging.basicConfig()configures the severity level, message format, filename, and file mode. - 3Log messages flow through Loggers, Handlers, and Formatters before reaching their destination.
- 4
os.path.join()and platform detection help construct paths that work on Windows and Unix-based systems. - 5A log file lets a developer inspect recorded activity after execution, including when the program displayed no output to the user.
At a glance
- Logging preserves debugging, information, warning, and critical messages in a file.
- The logging configuration includes severity level, message format, filename, and file mode.
- Loggers, Handlers, and Formatters form the route between a program message and its final destination.
os.path.join()with platform detection supports portable paths on Windows and Unix-based systems.- Post-execution log inspection enables program verification even when no output was shown to the user.