Controlling the Context Even More
00:00
Just like you did before, you’ll import the sys module to use stderr from there, and then you’ll import the logger object from the loguru module once more.
00:10
We’re going to use the .remove() method to remove all handlers, and just like you saw in the previous example, you’ll use the .add() method to add a new handler that will log to the stderr using a format of {time}, {level}, {message}, and {extra}.
00:28
Now instead of using .bind(), you’ll use the .contextualize() method, which acts as a Python context manager that temporarily adds a request ID to the logs.
00:40
Inside the with block, all log messages will automatically include this ID, but once you exit the block, the context is removed. Let’s log an info message,
00:54 for instance “Processing request”,
00:56 and another one which will be “Request completed”, for example. That’s very useful when handling web requests or database transactions where you want to group all logs related to a single operation.
01:11
Unlike .bind(), which creates a new logger instance, .contextualize() modifies the global logger context temporarily and then it restores it to its previous state.
01:22
Notice how each info message contains the request_id even though we didn’t give it manually to each one. If we were to log a new info message, for example a request that is processed but doesn’t have to do with the previous context, because it’s outside of the with block, you’ll notice that there’s no extra context information in there, there’s no request_id.
01:45
By combining both .bind() and .contextualize() methods, you can build a complete picture of what’s happening in your application. So let’s see an example that uses both of them.
01:58
Open a new Python interactive session and let’s do what you’ve been doing before, importing the sys module and importing the logger object from the loguru module.
02:08
Then using the .remove() method to remove all handlers and adding a new handler that we log to stderr with a format of {time}, {level}, {message}, and having the {extra} fields.
02:24
First you’ll use the .bind() method to include user_id = 123 to every log message. That will create a new logger, user_logger.
02:36
Now let’s use the .contextualize() method to include request _id to all the logs that are logged within the with block.
02:46
Use that user_logger to add an info log message that says “Processing user request”.
02:55
Notice how this info message has both the request_id and the user_id. But if you were to create another info message outside of this with block, you’ll notice that it only contains the user _id but not the request_id, because it’s not under the scope of the context manager.
03:17
What you did here is use the .bind() method to permanently bind user_id to track all actions for a specific user. And then you used the .contextualize() method to temporarily add request_id for the duration of a single operation.
03:33 This pattern is a great way to trace operations in web applications where you track both the user making requests and the individual request operations. In the next lesson, you’ll explore how to save your logs to files instead of sending them to standard error as you’ve been doing so far.
Become a Member to join the conversation.
