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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
"""
Configuration functions for the logging package.
"""
from __future__ import absolute_import
import logging
import sys
from . import buildlogger
from . import formatters
from . import loggers
_DEFAULT_FORMAT = "[%(name)s] %(message)s"
def using_buildlogger(logging_config):
"""
Returns true if buildlogger is set as a handler on the "fixture" or
"tests" loggers, and false otherwise.
"""
for logger_name in (loggers.FIXTURE_LOGGER_NAME, loggers.TESTS_LOGGER_NAME):
logger_info = logging_config[logger_name]
if _get_buildlogger_handler_info(logger_info) is not None:
return True
return False
def apply_config(logging_config):
"""
Adds all handlers specified by the configuration to the "executor",
"fixture", and "tests" loggers.
"""
logging_components = (loggers.EXECUTOR_LOGGER_NAME,
loggers.FIXTURE_LOGGER_NAME,
loggers.TESTS_LOGGER_NAME)
if not all(component in logging_config for component in logging_components):
raise ValueError("Logging configuration should contain %s, %s, and %s components"
% logging_components)
# Configure the executor, fixture, and tests loggers.
for component in logging_components:
logger = loggers.LOGGERS_BY_NAME[component]
logger_info = logging_config[component]
_configure_logger(logger, logger_info)
# Configure the buildlogger logger.
loggers._BUILDLOGGER_FALLBACK.addHandler(_fallback_buildlogger_handler())
def apply_buildlogger_global_handler(logger, logging_config, build_id=None, build_config=None):
"""
Adds a buildlogger.BuildloggerGlobalHandler to 'logger' if specified
to do so by the configuration.
"""
logger_info = logging_config[loggers.FIXTURE_LOGGER_NAME]
handler_info = _get_buildlogger_handler_info(logger_info)
if handler_info is None:
# Not configured to use buildlogger.
return
if all(x is not None for x in (build_id, build_config)):
log_format = logger_info.get("format", _DEFAULT_FORMAT)
formatter = formatters.ISO8601Formatter(fmt=log_format)
handler = buildlogger.BuildloggerGlobalHandler(build_id,
build_config,
**handler_info)
handler.setFormatter(formatter)
else:
handler = _fallback_buildlogger_handler()
# Fallback handler already has formatting configured.
logger.addHandler(handler)
def apply_buildlogger_test_handler(logger,
logging_config,
build_id=None,
build_config=None,
test_id=None):
"""
Adds a buildlogger.BuildloggerTestHandler to 'logger' if specified
to do so by the configuration.
"""
logger_info = logging_config[loggers.TESTS_LOGGER_NAME]
handler_info = _get_buildlogger_handler_info(logger_info)
if handler_info is None:
# Not configured to use buildlogger.
return
if all(x is not None for x in (build_id, build_config, test_id)):
log_format = logger_info.get("format", _DEFAULT_FORMAT)
formatter = formatters.ISO8601Formatter(fmt=log_format)
handler = buildlogger.BuildloggerTestHandler(build_id,
build_config,
test_id,
**handler_info)
handler.setFormatter(formatter)
else:
handler = _fallback_buildlogger_handler()
# Fallback handler already has formatting configured.
logger.addHandler(handler)
def _configure_logger(logger, logger_info):
"""
Adds the handlers specified by the configuration to 'logger'.
"""
log_format = logger_info.get("format", _DEFAULT_FORMAT)
formatter = formatters.ISO8601Formatter(fmt=log_format)
for handler_info in logger_info.get("handlers", []):
handler_class = handler_info["class"]
if handler_class == "logging.FileHandler":
handler = logging.FileHandler(filename=handler_info["filename"],
mode=handler_info.get("mode", "w"))
elif handler_class == "logging.NullHandler":
handler = logging.NullHandler()
elif handler_class == "logging.StreamHandler":
handler = logging.StreamHandler(sys.stdout)
elif handler_class == "buildlogger":
continue # Buildlogger handlers are applied when running tests.
else:
raise ValueError("Unknown handler class '%s'" % (handler_class))
handler.setFormatter(formatter)
logger.addHandler(handler)
def _fallback_buildlogger_handler():
"""
Returns a handler that writes to stderr.
"""
log_format = "[buildlogger:%(name)s] %(message)s"
formatter = formatters.ISO8601Formatter(fmt=log_format)
handler = logging.StreamHandler(sys.stderr)
handler.setFormatter(formatter)
return handler
def _get_buildlogger_handler_info(logger_info):
"""
Returns the buildlogger handler information if it exists, and None
otherwise.
"""
for handler_info in logger_info["handlers"]:
handler_info = handler_info.copy()
if handler_info.pop("class") == "buildlogger":
return handler_info
return None
|