Created
September 12, 2017 09:49
-
-
Save 9seconds/11cce860c45b009e55199081ccfd54fc to your computer and use it in GitHub Desktop.
log.py example
This file contains 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
# -*- coding: utf-8 -*- | |
# vim: set et sw=4 ts=4: | |
import logging | |
import logging.config | |
import flask | |
class RequestIDLogger(logging.getLoggerClass()): | |
def _log(self, level, msg, args, exc_info=None, extra=None): | |
try: | |
request_id = getattr(flask.g, "request_id", None) | |
except RuntimeError: # outside of Flask context | |
request_id = None | |
if request_id is not None: | |
msg = "<request:{0}> | {1}".format(request_id, msg) | |
return super(RequestIDLogger, self)._log( | |
level, msg, args, exc_info, extra | |
) | |
logging.setLoggerClass(RequestIDLogger) | |
getLogger = logging.getLogger | |
configure_logging = logging.config.dictConfig | |
DEBUG = logging.DEBUG | |
INFO = logging.INFO |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment