1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
# 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)
|