Source code

Revision control

Other Tools

1
import logging
2
3
from mozlog import commandline, stdadapter, set_default_logger
4
from mozlog.structuredlog import StructuredLogger, log_levels
5
6
7
def setup(args, defaults, formatter_defaults=None):
8
logger = args.pop('log', None)
9
if logger:
10
set_default_logger(logger)
11
StructuredLogger._logger_states["web-platform-tests"] = logger._state
12
else:
13
logger = commandline.setup_logging("web-platform-tests", args, defaults,
14
formatter_defaults=formatter_defaults)
15
setup_stdlib_logger()
16
17
for name in args.keys():
18
if name.startswith("log_"):
19
args.pop(name)
20
21
return logger
22
23
24
def setup_stdlib_logger():
25
logging.root.handlers = []
26
logging.root = stdadapter.std_logging_adapter(logging.root)
27
28
29
class LogLevelRewriter(object):
30
"""Filter that replaces log messages at specified levels with messages
31
at a different level.
32
33
This can be used to e.g. downgrade log messages from ERROR to WARNING
34
in some component where ERRORs are not critical.
35
36
:param inner: Handler to use for messages that pass this filter
37
:param from_levels: List of levels which should be affected
38
:param to_level: Log level to set for the affected messages
39
"""
40
def __init__(self, inner, from_levels, to_level):
41
self.inner = inner
42
self.from_levels = [item.upper() for item in from_levels]
43
self.to_level = to_level.upper()
44
45
def __call__(self, data):
46
if data["action"] == "log" and data["level"].upper() in self.from_levels:
47
data = data.copy()
48
data["level"] = self.to_level
49
return self.inner(data)
50
51
52
class LoggedAboveLevelHandler(object):
53
"""Filter that records whether any log message above a certain level has been
54
seen.
55
56
:param min_level: Minimum level to record as a str (e.g., "CRITICAL")
57
58
"""
59
def __init__(self, min_level):
60
self.min_level = log_levels[min_level.upper()]
61
self.has_log = False
62
63
def __call__(self, data):
64
if (data["action"] == "log" and
65
not self.has_log and
66
log_levels[data["level"]] <= self.min_level):
67
self.has_log = True