Skip to content

Instantly share code, notes, and snippets.

@mariocesar
Created July 27, 2015 13:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mariocesar/858d84b51e0c3a069b78 to your computer and use it in GitHub Desktop.
Save mariocesar/858d84b51e0c3a069b78 to your computer and use it in GitHub Desktop.
WSGI Python middleware to simple log all requests and responses to later debug.
class RequestLogMiddleware(object):
"""
Log requests to the application, a simple wsgi middleware.
"""
def __init__(self, application):
self.application = application
def __call__ (self, environ, start_response):
context = {}
self.context['script_name'] = environ.get('HTTP_X_SCRIPT_NAME', '')
self.context['errors'] = environ['wsgi.errors']
self.context['url'] = environ['wsgi.url_scheme'] + '://' + environ['HTTP_HOST'] + environ['PATH_INFO']
if environ['QUERY_STRING']:
self.context['url'] += '?' + environ['QUERY_STRING']
self.do_log_request(environ, context)
response = self.application(environ, start_response)
self.do_log_response(start_response, context)
return response
def do_log_request(self, environ, context):
pass
def do_log_response(self, response, context):
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment