Skip to content

Instantly share code, notes, and snippets.

@jaytaylor
Last active July 25, 2022 12:22
Show Gist options
  • Save jaytaylor/5457210 to your computer and use it in GitHub Desktop.
Save jaytaylor/5457210 to your computer and use it in GitHub Desktop.
MyFancyRequestHandler extends the werkzeug BaseRequestHandler to provide HTTP request execution time to the server log. NB: The associated tutorial is: http://jaytaylor.com/blog/?p=305
import time
from werkzeug.serving import BaseRequestHandler
class MyFancyRequestHandler(BaseRequestHandler):
"""Extend werkzeug request handler to suit our needs."""
def handle(self):
self.fancyStarted = time.time()
rv = super(MyFancyRequestHandler, self).handle()
return rv
def send_response(self, *args, **kw):
self.fancyProcessed = time.time()
super(MyFancyRequestHandler, self).send_response(*args, **kw)
def log_request(self, code='-', size='-'):
duration = int((self.fancyProcessed - self.fancyStarted) * 1000)
self.log('info', '"{0}" {1} {2} [{3}ms]'.format(self.requestline, code, size, duration))
@alex-yell
Copy link

Like @noise, I'm trying to accomplish this from gunicorn, which doesn't app.run. The Heroku Procfile doesn't help either. There doesn't seem to be a way to set this handler after/before the call to run.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment