Skip to content

Instantly share code, notes, and snippets.

@9seconds
Created September 12, 2017 09:49
Show Gist options
  • Save 9seconds/11cce860c45b009e55199081ccfd54fc to your computer and use it in GitHub Desktop.
Save 9seconds/11cce860c45b009e55199081ccfd54fc to your computer and use it in GitHub Desktop.
log.py example
# -*- 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