使用用户/ip进行Django日志记录 [英] Django logging with user/ip
问题描述
我正在使用日志记录"模块记录大量消息.我想在日志中添加用户"(request.user).但是,尽管它在视图功能中可用,但我不想将其传递给所有帮助程序.
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.
有人知道这种方法吗?[我一直在想办法追踪,直到找到一个在参数中带有"request"的函数为止.
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]
预先感谢
推荐答案
执行此操作的方法是将请求(或request.user)粘贴在
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.
这篇关于使用用户/ip进行Django日志记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!