Python logging
Logging levels allow you to control which messages you record in your logs. Think of log levels as verbosity levels. How granular do you want your logs to be?
Do you want to only log exceptions? Or would you like to log information and warnings too? You get to set all of these things using Python's logging module.
In this chapter, you will learn about the following:
While this chapter will be pretty short, log levels are an important topic.
Available log levels
Python's logging module comes with six logging levels built-in.
Here's a list of those log levels, from the lowest precedence to the highest:
Each log level maps to a numeric value, the integer in parentheses above. Unless you plan to create your own custom log levels, you don't need to know anything about those numeric values.
But to define your custom log levels, you need to understand something. If you use the same numeric value as one of the pre-defined log levels, you will overwrite that level with your new one, which may have unintended consequences in your code.
So be careful!
Using log levels
Now that you know what a log level is and what you use it for, you must learn how to write the actual logging code!
Open up your Python editor of choice and create a new file named log_levels.py.
Then enter the following code:
Here, you tell Python's logger to write to a new file named log_levels.log. Then, you write five different log messages using the five main log levels.
When you run this code, it will write out the following text to your log file:
To learn more about how setting a log level works, try changing the level you log at. But first, delete the original log, or you'll end up appending more data to it.
Now that you have deleted the log file, log_levels.log, change the level parameter in your Python script from logging.DEBUG to logging.WARNING.
Then re-run the code and open your new log. It should have the following information in it:
By changing the log level, you control the granularity of the logs you save to your log file. You could add a command line interface or a graphical user interface to your application, allowing you to enable different log levels in your code to help debug.
For now, try changing the log level specified in the example code provided in this chapter. All you'll need to do is re-run the code each time.
Remember this: if you do not remove the log file before you run the code, the new run will append to it. This can be potentially confusing since the log will repeatedly have the same message.
Logging Exceptions
You might think logging an error or a critical log message is the same as logging an exception, but you'd be mistaken. Python's logging module lets you log an exception using the exception() method on the logger object. When you call that method, it will log the entire traceback raised when the exception occurred.
An example will help clarify what that vague statement means. Open up your Python IDE or text editor and create a new file named log_exception.py. Then enter the following code:
The code above will catch two types of errors:
When you use this code, you will hit the first exception handler and get the following output logged to your terminal or console:
An error has occurred!
Traceback (most recent call last):
File "C:\code\log_exception.py", line 10, in divide
result = a / b
~~^~~
ZeroDivisionError: division by zero
Logging exceptions allows you to troubleshoot your application and determine what went wrong. It is also a great tool for detecting bad user input.
Wrapping Up
Now that you know how log levels work in Python's logging module, you should be able to set the root logger's log level to whichever level you want to log.
Wait a minute! Why does this only apply to the root logger? Don't worry. You will learn how to create your custom logger soon. But it's good to learn the basics using the root logger, as it uses less code and is easier to understand.
You can use the knowledge you've gained in this chapter to change the granularity of your logs. When you learn about creating your logger, you can apply the concepts you learned in this chapter to your custom logger.
Python has great documentation about logging levels that you can refer to for additional information.