We can track events in a software application, this is known as logging. Let’s start with a simple example, we will log a warning message.
As opposed to just printing the errors, logging can be configured to disable output or save to a file. This is a big advantage to simple printing the errors.
Complete Python Bootcamp: Go from zero to hero in Python
This will output:
WARNING:root:This is a warning!
We can easily output to a file:
The importance of a log message depends on the severity.
Level of severity
The logger module has several levels of severity. We set the level of severity using this line of code:
These are the levels of severity:
|DEBUG||Information only for problem diagnostics|
|INFO||The program is running as expected|
|WARNING||Indicate something went wrong|
|ERROR||The software will no longer be able to function|
|CRITICAL||Very serious error|
Time in log
You can enable time for logging using this line of code:
An example below:
2015-06-25 23:24:01,153 Logging app started