Django logging with user/ip

假如想象 提交于 2021-01-27 12:54:56

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!