Created
June 14, 2011 13:25
-
-
Save javisantana/1024886 to your computer and use it in GitHub Desktop.
small context manager to log timing on http request (or whatever)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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