Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Log Customization

00:00 In this lesson, you’re going to learn how to customize the structure of your logs in loguru. Loguru works great out of the box, but in real projects, you usually want more control over how your logs look and what information they show. So let’s start with the most minimal setup possible, just the essential log message without any additional information or metadata.

00:23 First, let’s import the sys module to be able to define stderr as the sink. Then you’ll import the logger object from the loguru module. As we saw in the previous video, you’ll remove the default handler to start from a clean state using the .remove() method on the logger object. Then you’ll add a new handler to log messages very simplistically.

00:52 loguru makes the customization straightforward using the .add() method with the format parameter. So we’ll use the {message} placeholder to display just the log message with no other information.

01:06 So let’s log an info message, and as you can see, the output is extremely clean. There are no timestamps, no log levels, and no extra metadata. This kind of setup can be very useful when you want the logs to be as concise as possible, but for most cases it’ll be too minimalistic.

01:28 loguru lets you control how logs look using placeholders. Each placeholder gets replaced at runtime with real information about what’s happening in the code.

01:38 The {time} placeholder shows the timestamp when the log was created. The {level} placeholder tells you how important the message is, in other words, the log level.

01:50 The {message} placeholder is the actual content that you wrote in your code. And the {name} placeholder points to the module where the log comes from, while the {line} placeholder shows the exact line number the log comes from.

02:04 So let’s go back to the console. For better log analysis, you’ll typically want to include at least a timestamp and a log level in your format string. So let’s see how we’ll do that.

02:17 First of all, we’ll use the .remove() method to start from a clean state and remove all handlers. And next, we’ll use the .add() method.

02:26 We’ll give the first argument sys.stderr, the standard error, and then in the format we’ll use a different version this time. We’ll use the {time} placeholder and you can customize it even further by passing the format string to show the full date and time in a readable format.

02:46 You’ll use pipe characters to create visual separation so that the logs are easier to scan when troubleshooting. Then you’ll use the {level} placeholder to indicate the severity and then another pipe for separation and lastly the {message} placeholder for your actual log content. So if you log an info message, it’ll look like this.

03:14 See how now we have the timestamp, the log level, and the message that we wanted to log. To help with debugging, you might also want to include the module name and the line number to see exactly where a log message originated.

03:30 So let’s remove all the handlers and let’s add a new one.

03:37 We’ll modify the format that we’re giving it.

03:44 So this time we’re using the {time} placeholder but we customize it by using a format string not to include the date but only the time. Notice how in this case we’re using the greater than symbol twice instead of the pipe to create that separation in between the elements of the log message. Then we use the {name} placeholder to get the module name, colon as a separator, and then the {line} placeholder to indicate the line number.

04:16 If you log a warning message now, you’ll notice the date is no longer included and it now shows the module name and the line number where the warning occurred.

04:26 Next, you’ll learn how to add additional information to your logs.

Become a Member to join the conversation.