Working With Log Levels
00:00
In this lesson, you’ll learn more about log levels. Log levels define how important a log message is, so it helps you control what gets logged and where. loguru provides seven built-in log levels, each with its own method, severity value, and default color scheme.
00:19 Let’s review each of them from lowest to highest severity. First, we have TRACE, which corresponds to the severity value of 5. This is the most detailed level.
00:29
You use the .trace() method when you want extremely detailed information. It’s mainly used for debugging or trying to understand complex execution flows.
00:39
Then you have DEBUG, with a severity value of 10. This is the level you rely on when you’re building or fixing things. Next is INFO, with a severity value of 20, and you use the .info() method to log what the application is doing at any point in time.
00:56
Then we have SUCCESS, that has a severity value of 25. The .success() method is used when something important worked exactly as expected. Next up is WARNING, with a severity value of 30. The .warning() method is used when something unexpected happens, but your code is able to still run.
01:15
It’s unexpected, but it’s not an error. Then we have ERROR, with a severity value of 40. The .error() method is used when something breaks, but the software can continue running.
01:27
Next up, we have CRITICAL, with a severity value of 50. This is the highest severity level, and you use the .critical() method when the system is in serious trouble and immediate action is required.
01:41
Let’s go to the terminal and try to log a TRACE message. First of all, we need to import the logger object from loguru’s module.
01:51
So we’ll do from loguru import logger. Next, let’s use the logger object and the .trace() method to log a TRACE message that is called “This is a detailed trace message!” And you’ll notice that nothing happened.
02:08
However, if we try to log a DEBUG message, it will appear in the output. The reason why a TRACE message didn’t appear in the output is because by default, loguru shows all messages with level 10, which corresponds to DEBUG and above.
02:27
But TRACE has a severity level of 5. To enable TRACE-level messages, you can adjust the minimum level using the .add() method from the logger object.
02:40
The .add() method adds a new handler. A handler defines what happens to your logs after you create them. The idea is to send them to the standard error stream.
02:51
So first of all, you’ll need to import the sys module to access sys.stderr, which will be used as a sink or output destination for your logs.
03:03
To create that new handler, you’ll have to provide two arguments to the .add() method. The first one is a sink or output destination for your logs, which in this case is sys.stderr.
03:16 The second one is the minimum severity level from which log messages should be sent to the sink.
03:23
And the .add() method returns an integer that uniquely identifies that handler. You can use this identifier to later remove the handler using the .remove() method.
03:36 So now let’s try to log a TRACE message.
03:42 And you’ll notice that now the message is visible. And the same if we try to log a DEBUG message.
03:51
However, you may notice something unexpected happened. The DEBUG message appears twice in the output. This happens because loguru now has two handlers, the default one with the DEBUG level and the new one with the TRACE level.
04:08
To fix this, you’ll need to remove the default handler first. And for that, we’ll use the .remove() method. When we call the .remove() method without any argument, it’ll remove all existing handlers, including the default one. So this allows you to start with a clean state before adding your new handler. And let’s add that new handler right now.
04:34 So then, when we try to log a TRACE message, we’ll be able to see it.
04:42 And when we log a DEBUG message, we’ll also be able to see it, but just once as we needed.
04:51 Understanding log levels helps you implement appropriate logging strategies for different environments. For example, you might use the DEBUG level in development, but only WARNING and above in production.
05:04 Next, you’ll look at how to customize the format and filtering of your log messages to make them even more useful.
Become a Member to join the conversation.
