class LoggingMiddleware(object):

    def __init__(self, app):
        self._app = app

def __call__(self, environ, resp):
    errorlog = environ['wsgi.errors']
pprint.pprint(('REQUEST', environ), stream=errorlog)

def log_response(status, headers, *args):
    pprint.pprint(('RESPONSE', status, headers), stream=errorlog)
return resp(status, headers, *args)
return self._app(environ, log_response)
Print raw HTTP request in Flask or WSGI