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

Adding Context

00:00 To make your logs more useful, you’ll often want to include extra content in them, such as user IDs, request IDs, or session information to understand who did something.

00:11 Loguru provides several ways to add this context. The simple approach is to pass keyword arguments directly into your logging methods. Let’s see how to do that.

00:23 Open an interactive Python session, and we’re going to import the sys module to be able to use stderr as we’ve been doing in the previous lessons, and then let’s import the logger object from the loguru module.

00:37 Then you’ll remove all handlers with the .remove() method, and then you’ll add a handler with a format that uses the {time} placeholder, the {level}, and the {message} placeholders that we saw before, plus a new placeholder called {extra}, with a dict of attributes that add contextual data to our log messages.

01:01 This way, when you log an info message, you can pass a keyword argument such as user_id=123 to logger.info() to add extra content.

01:12 This approach is perfect for one-off contextual information that you only need to use for a specific log entry. The context is automatically added to the extra dictionary. But if we log another info message without having that info, it won’t be there.

01:32 And that’s actually the advantage of this approach. You can attach contextual data only when it’s relevant, without cluttering every log message with unnecessary information.

01:49 For context that needs to persist across multiple log entries, loguru provides two different options. The .bind() method is useful when you want to attach persistent information to every log message from a specific logger instance, such as user IDs, session IDs, or server information.

02:10 This saves you from manually including this information in each log message and ensures consistency across all logs from that particular logger.

02:20 Another option is .contextualize(), which temporarily adds context within a code block using a context manager. The .contextualize() method provides a clean way to add temporary context that automatically gets cleaned up when you decide. Let’s see both of them in action.

02:39 Let’s go back to the code that we had and see them in action, starting with the .bind() method. When you use the .bind() method, it’ll create a new logger instance that you’ll save in a variable called user_logger.

02:54 And we give the .bind() method the argument which is what it should attach to all messages, in this case a user_id of 123. Every log message from the user_logger will automatically include this user ID in the extra fields, and this helps trace all actions performed by a certain user.

03:16 So if we use that new user_logger to log an info message, you’ll notice that each log message includes the bound context in its output and you don’t need to specify it each time.

03:29 However, this context is permanent and that isn’t always ideal. Sometimes you only need to add context that’s relevant to a specific operation or section of the code, such as request IDs during an API call or transaction IDs in a database operation.

03:47 That’s where .contextualize() comes in. Let’s start with a new interactive Python session to see how .contextualize() works.

Become a Member to join the conversation.