Question

I'd like to prepend the user email to all web app logs.

I can store the email (taken from the cookie and such) in threading.local(). But I can't be always sure the variable will be there in the thread locals.

Is there a way to tell all the loggers in my app to act like that?

Was it helpful?

Solution

You can create a logging.Filter() object that grabs the thread-local variable (or a suitable default when its not there) and add it to the log record. Attach that filter to the root logger and it will be called for all log records before they are passed to handlers. Once the variable is in the log record it can be used in the formatters you use to display/save the information.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top