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
5
6
7
def setup(args, defaults):
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
setup_stdlib_logger()
15
16
for name in args.keys():
17
if name.startswith("log_"):
18
args.pop(name)
19
20
return logger
21
22
23
def setup_stdlib_logger():
24
logging.root.handlers = []
25
logging.root = stdadapter.std_logging_adapter(logging.root)
26
27
28
class LogLevelRewriter(object):
29
"""Filter that replaces log messages at specified levels with messages
30
at a different level.
31
32
This can be used to e.g. downgrade log messages from ERROR to WARNING
33
in some component where ERRORs are not critical.
34
35
:param inner: Handler to use for messages that pass this filter
36
:param from_levels: List of levels which should be affected
37
:param to_level: Log level to set for the affected messages
38
"""
39
def __init__(self, inner, from_levels, to_level):
40
self.inner = inner
41
self.from_levels = [item.upper() for item in from_levels]
42
self.to_level = to_level.upper()
43
44
def __call__(self, data):
45
if data["action"] == "log" and data["level"].upper() in self.from_levels:
46
data = data.copy()
47
data["level"] = self.to_level
48
return self.inner(data)