zuul/zuul/lib/logutil.py
James E. Blair 482338f70c Identify cherrypy requests in logs
This adds a unique ID to every cherrypy request and adds it to the
all of the log lines in the web server.

Some requests produce multiple log entries, and this will allow us
to associate them with each other as well as the cherrpy access log
which is emitted at the end of the request.

To add this to cherrypy's request logging, we subclass the cherrypy
LogManager class and add accessor methods so that whenever the
superclass requests a logger, it gets our annotated logger.

See: https://github.com/cherrypy/cherrypy/blob/main/cherrypy/_cplogging.py

There is an underscore in that module name, but several parts of the
cherrpy documentation suggest importing underscore modules for the
purpose of extending cherrpy.

On the ZuulApi class, we add a similar accessor method for obtaining
the annotated logger for any request.

The two log streaming classes initialize their log attribute in the
constructor since their instances are dedicated to a single request.

The remaining classes retain the class-level logger since they operate
outside of the request context.

Change-Id: I66af717ab34fa4ad3492a01badb25e3865133104
2022-02-02 16:16:27 -08:00

83 lines
2.6 KiB
Python

# Copyright 2019 BMW Group
# Copyright 2021 Acme Gating, LLC
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import logging
def get_annotated_logger(logger, event, build=None, request=None):
# Note(tobiash): When running with python 3.5 log adapters cannot be
# stacked. We need to detect this case and modify the original one.
if isinstance(logger, EventIdLogAdapter):
extra = logger.extra
else:
extra = {}
if event is not None:
if hasattr(event, 'zuul_event_id'):
extra['event_id'] = event.zuul_event_id
else:
extra['event_id'] = event
if build is not None:
extra['build'] = build
if request is not None:
extra['request'] = request
if isinstance(logger, EventIdLogAdapter):
return logger
return EventIdLogAdapter(logger, extra)
class EventIdLogAdapter(logging.LoggerAdapter):
def process(self, msg, kwargs):
msg, kwargs = super().process(msg, kwargs)
extra = kwargs.get('extra', {})
event_id = extra.get('event_id')
build = extra.get('build')
request = extra.get('request')
new_msg = []
if event_id is not None:
new_msg.append('[e: %s]' % event_id)
if build is not None:
new_msg.append('[build: %s]' % build)
if request is not None:
new_msg.append('[req: %s]' % request)
new_msg.append(msg)
msg = ' '.join(new_msg)
return msg, kwargs
def addHandler(self, *args, **kw):
return self.logger.addHandler(*args, **kw)
class MultiLineFormatter(logging.Formatter):
def format(self, record):
rec = super().format(record)
ret = []
# Save the existing message and re-use this record object to
# format each line.
saved_msg = record.message
for i, line in enumerate(rec.split('\n')):
if i:
record.message = ' ' + line
ret.append(self.formatMessage(record))
else:
ret.append(line)
# Restore the message
record.message = saved_msg
return '\n'.join(ret)