Skip to content

Instantly share code, notes, and snippets.

@javisantana
Created June 14, 2011 13:25
Show Gist options
  • Select an option

  • Save javisantana/1024886 to your computer and use it in GitHub Desktop.

Select an option

Save javisantana/1024886 to your computer and use it in GitHub Desktop.
small context manager to log timing on http request (or whatever)
timing_logger = logging.getLogger('something')
@contextmanager
def timing(name):
t1 = datetime.now()
try:
yield
finally:
t1 = datetime.now() - t1
t1 = t1.seconds + t1.microseconds/1e6
timing_logger.info(name , exc_info=sys.exc_info(), extra={'data': {
'seconds': "%.2f" % t1,
'when': datetime.now()
}})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment