mirror of
https://github.com/sprockets/sprockets.mixins.correlation.git
synced 2024-11-23 11:19:53 +00:00
Create new log_function to add CID to tornado logs
This commit is contained in:
parent
05cccbddf0
commit
f917579019
1 changed files with 28 additions and 0 deletions
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue