问题
I am using the "logging" module to log a very large amount of messages. I would like to add "user" (request.user) to the log. But while it is available in the view function, I don't want to pass it to all helpers.
Does anyone know a way to this ? [I was thinking of maybe somehow walking the trace until I find a function with "request" in its args]
Thanks in advance
回答1:
The hacky way to do this is to stick the request (or request.user) in threadlocal storage.
The correct and maintainable way is to explicitly pass the data you need into the functions that need it. If doing this seems too onerous, it may reveal a deeper problem in how you've structured your code.
来源:https://stackoverflow.com/questions/1854654/django-logging-with-user-ip