Skip to content
This repository has been archived by the owner on Mar 24, 2021. It is now read-only.

Commit

Permalink
Add request_id to all log records
Browse files Browse the repository at this point in the history
Use a filter to add a request_id to all log records so we can trace
requests throughout the stack. Also update the key to match that used by
nginx logs
  • Loading branch information
timmow committed Aug 12, 2015
1 parent 49721db commit a7a2611
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion backdrop/core/log_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,18 @@
from flask import request


class RequestIdFilter(logging.Filter):
def filter(self, record):
try:
record.govuk_request_id = request.headers.get('Govuk-Request-Id')
except RuntimeError:
# flask will throw a runtime error if we are attempting to get the
# header outside of the application context. In this case we can't
# infer the request_id, so we can just pass
pass
return True


def get_log_file_handler(path, log_level=logging.DEBUG):
handler = FileHandler(path)
handler.setFormatter(logging.Formatter(
Expand Down Expand Up @@ -33,6 +45,8 @@ def set_up_logging(app, env):
get_json_log_handler("log/%s.log.json" % env, app.name)
)
logger.setLevel(numeric_log_level)
request_id_filter = RequestIdFilter()
app.logger.addFilter(request_id_filter)
app.logger.info("{} logging started".format(app.name))
app.logger.info("{} logging started".format(numeric_log_level))
app.before_request(create_request_logger(app))
Expand Down Expand Up @@ -67,4 +81,4 @@ def log_response(response):


def create_logging_extra_dict():
return {'request_id': request.headers.get('Request-Id')}
return {'govuk_request_id': request.headers.get('Govuk-Request-Id')}

0 comments on commit a7a2611

Please sign in to comment.