5
mozbuild is a Python package providing functionality used by Mozilla's
11
* mozbuild.compilation -- Functionality related to compiling. This
12
includes managing compiler warnings.
13
* mozbuild.logging -- Defines mozbuild's logging infrastructure.
14
mozbuild uses a structured logging backend.
19
One of the features of mozbuild is structured logging. Instead of
20
conventional logging where simple strings are logged, the internal
21
logging mechanism logs all events with the following pieces of
25
* A dict of log message fields
28
Essentially, instead of assembling a human-readable string at
29
logging-time, you create an object holding all the pieces of data that
30
will constitute your logged event. For each unique type of logged event,
31
you assign an *action* name.
33
Depending on how logging is configured, your logged event could get
34
written a couple of different ways.
39
Where machines are the intended target of the logging data, a JSON
40
logger is configured. The JSON logger assembles an array consisting of
41
the following elements:
43
* Decimal wall clock time in seconds since UNIX epoch
44
* String *action* of message
45
* Object with structured message data
47
The JSON-serialized array is written to a configured file handle.
48
Consumers of this logging stream can just perform a readline() then feed
49
that into a JSON deserializer to reconstruct the original logged
50
message. They can key off the *action* element to determine how to
51
process individual events. There is no need to invent a parser.
57
Where humans are the intended consumer of a log message, the structured
58
log message are converted to more human-friendly form. This is done by
59
utilizing the *formatting* string provided at log time. The logger
60
simply calls the *format* method of the formatting string, passing the
61
dict containing the message's fields.
63
When *mach* is used in a terminal that supports it, the logging facility
64
also supports terminal features such as colorization. This is done
65
automatically in the logging layer - there is no need to control this at
68
In addition, messages intended for humans typically prepends every line
69
with the time passed since the application started.
74
Structured logging piggybacks on top of Python's built-in logging
75
infrastructure provided by the *logging* package. We accomplish this by
76
taking advantage of *logging.Logger.log()*'s *extra* argument. To this
77
argument, we pass a dict with the fields *action* and *params*. These
78
are the string *action* and dict of message fields, respectively. The
79
formatting string is passed as the *msg* argument, like normal.
81
If you were logging to a logger directly, you would do something like:
83
logger.log(logging.INFO, 'My name is {name}',
84
extra={'action': 'my_name', 'params': {'name': 'Gregory'}})
86
The JSON logging would produce something like:
88
[1339985554.306338, "my_name", {"name": "Gregory"}]
90
Human logging would produce something like:
92
0.52 My name is Gregory
94
Since there is a lot of complexity using logger.log directly, it is
95
recommended to go through a wrapping layer that hides part of the
96
complexity for you. e.g.
98
def log(self, level, action, params, format_str):
99
self.logger.log(level, format_str,
100
extra={'action': action, 'params': params)