Loading video player…

Understanding Logging Basics

00:00 In this lesson, you’ll learn about the fundamentals of logging with loguru. We’ll use the python command to open an interactive Python session.

00:09 Now, loguru gives us a pre-configured logger, so we don’t need to set anything up before using it. All we have to do is import it. And we do that by doing from loguru import logger.

00:24 And that’s it! We can already log messages. So let’s start with a debug message. To do that, we’ll do logger.debug(), and then we’re going to give our debug message.

00:37 So, for instance, just “Debug message”. Notice how the message is formatted with useful information, like the timestamp, the log level, and the location of the log call. And we get this formatted log message, but we didn’t configure anything yet.

00:56 loguru also gives us different log levels, like info. And the way we do it is very similar, so logger.info(), and this would be an info message.

01:10 Notice how the first one is a DEBUG message, and the second one is an INFO message. We also have error messages, which we can log by doing logger.error() and “Error message”.

01:26 Each of these levels is color-coded. So DEBUG is blue, INFO is white, and ERROR is red. This makes it so much easier to scan logs very quickly.

01:38 If your terminal doesn’t support colors, loguru will automatically turn them off.

01:44 Now, let’s dive deeper into the structure of these log messages. If we read the log message from left to right, we first see the timestamp. And this one has millisecond precision.

01:58 Then we see DEBUG, which is the log level that indicates how severe this log is.

02:07 After that, we see where the log came from, the module, the function, and the line number. And finally, the message itself. Each part of this log message is separated by symbols like pipes or dashes to keep everything readable. And you’ll learn how to customize this later in the course.

02:30 By default, loguru sends the logs to standard error or stderr. That’s why we see everything directly in the terminal. But later in the course, we’ll learn how to send logs to files and even to multiple destinations at the same time.

02:47 For now, the key takeaway is import the logger and start logging. That’s it. In the next lesson, we’ll talk about log levels, what they mean, and when to use each one.

Become a Member to join the conversation.