OLD | NEW |
| (Empty) |
1 # Logging in infra.git | |
2 | |
3 ## Features | |
4 | |
5 The `infra_libs.logs` package contains some code to simplify logging and | |
6 make it consistent and easily configurable. Using it makes your code | |
7 future-proof. | |
8 | |
9 Offered features: | |
10 | |
11 * log level can be changed from the command-line. | |
12 * too verbose modules can be blacklisted for easier debugging. | |
13 * ensures a consistent log format. | |
14 | |
15 A typical log line looks like: | |
16 | |
17 [I2014-06-27T11:42:32.418716-07:00 7082 logs:71] this is the message | |
18 | |
19 The first letter gives the severity of the message, followed by a | |
20 timestamp with timezone information (iso8601), the process id, the | |
21 current module name, and the thread id. After the closing square bracket | |
22 comes the actual message. | |
23 | |
24 ## Sample code | |
25 | |
26 This is the standard way to set up logging so as to take advantage of | |
27 the goodness provided by `infra_libs.logs`. | |
28 | |
29 In top-level files (other example in | |
30 [infra.services.sysmon.__main__](../../infra/services/sysmon/__main__.py)): | |
31 | |
32 ```python | |
33 import argparse | |
34 import infra_libs.logs | |
35 | |
36 parser = argparse.ArgumentParser() | |
37 infra_libs.logs.add_argparse_options(parser) | |
38 | |
39 options = parser.parse_args() | |
40 infra_libs.logs.process_argparse_options(options) | |
41 ``` | |
42 | |
43 Logging messages should be done this way (other example in | |
44 `infra.libs.service_utils.outer_loop`): | |
45 | |
46 ```python | |
47 import logging | |
48 LOGGER = logging.getLogger(__name__) | |
49 | |
50 LOGGER.info('great message') | |
51 LOGGER.error('terrible error') | |
52 ``` | |
53 | |
54 Using `logging.getLogger` is a good practice in general (not restricted to | |
55 using infra_libs.logs) because it allows for module blacklisting and | |
56 other goodness. It should be done at import time. See also the official | |
57 [logging HOWTO](https://docs.python.org/2/howto/logging.html). | |
58 `infra_libs.logs` also formats the output of the root logger, but using | |
59 this logger is not recommended. | |
OLD | NEW |