From f9175790190fa3e364d96887c64ec1237e199250 Mon Sep 17 00:00:00 2001 From: Kelly O'Brien Date: Wed, 3 Jun 2015 10:01:16 -0400 Subject: [PATCH] Create new log_function to add CID to tornado logs --- sprockets/mixins/correlation/mixins.py | 28 ++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/sprockets/mixins/correlation/mixins.py b/sprockets/mixins/correlation/mixins.py index 87986ac..6d87db3 100644 --- a/sprockets/mixins/correlation/mixins.py +++ b/sprockets/mixins/correlation/mixins.py @@ -81,3 +81,31 @@ class HandlerMixin(object): """ return self.request.headers.get(name, default) + + +def correlation_id_logger(handler): + """ Custom Tornado access log writer that appends correlation-id. + + This function can be used to append the coorelation-id to the + Tornado access logs. To use, simply set the value of the + log_function kwarg of the Tornado Application constructor to this + function. + + *Example* + web.Application([], log_function=correlation_id_logger) + + :param tornado.web.RequestHandler handler: the request handler that + is processing the client request. + """ + if handler.get_status() < 400: + log_method = log.access_log.info + elif handler.get_status() < 500: + log_method = log.access_log.warning + else: + log_method = log.access_log.error + request_time = 1000.0 * handler.request.request_time() + correlation_id = getattr(handler, "correlation_id", None) + if correlation_id is None: + correlation_id = handler.request.headers.get('Correlation-ID', None) + log_method("%d %s %.2fms {CID %s}", handler.get_status(), + handler._request_summary(), request_time, correlation_id)