logging request info to file, in inner functions (Django)
I am using simple logger to save debug info to file. My problem is I want each line in the log to be prefixed with request info (eg. user, url), and the inner functions activated by the view do not have access to the request parameter.
I thought about
- passing request down to every function - very bad
- climbing up the traceback stack, searching for "request" in local variables - not sure how to do this or if it will be to开发者_JS百科o slow (say, logging up to ~100 times per request)
- surrounding the view function with a "with" statement that will keep all the request info in a global variable (key= thread id) - not recommended as security risk (http://code.djangoproject.com/wiki/CookBookThreadlocalsAndUser) and requires indenting the entire code in the view
Is there an elegant solution?
I believe you want to use a filter that adds request attributes to your log messages. This package seems to address this: http://pypi.python.org/pypi/django-requestlogging
This may not be exactly what you're looking for, but django-sentry provides the functionality you describe(with a nice web interface also). It might be easier than trying to roll your own solution.
精彩评论