Repository navigation
Expand file tree
/
Copy pathlogging_setup.py
More file actions
74 lines (55 loc) · 2.53 KB
/
Copy pathlogging_setup.py
File metadata and controls
74 lines (55 loc) · 2.53 KB
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
"""`logging` — levels, handlers, and why `print` does not scale.
The model has four parts: a **logger** (named, hierarchical) creates records, a
**handler** decides where they go, a **formatter** decides how they look, and a
**filter** can drop them. Libraries create loggers and nothing else;
applications configure handlers. That separation is why a library must never
call `basicConfig`.
Use `logger.exception(...)` inside an `except` block: it logs at ERROR *and*
attaches the traceback. And pass arguments lazily — `logger.info("x=%s", x)`
formats only if the message is actually emitted.
"""
import logging
import sys
class RequestFilter(logging.Filter):
"""Attach contextual data to every record passing through."""
def __init__(self, request_id: str) -> None:
super().__init__()
self.request_id = request_id
def filter(self, record: logging.LogRecord) -> bool:
record.request_id = self.request_id
return True
def configure() -> logging.Logger:
logger = logging.getLogger("demo")
logger.setLevel(logging.DEBUG)
logger.handlers.clear()
console = logging.StreamHandler(sys.stdout)
console.setLevel(logging.INFO) # a handler can be stricter than the logger
console.setFormatter(
logging.Formatter(" %(levelname)-8s %(name)s [%(request_id)s] %(message)s")
)
console.addFilter(RequestFilter("req-42"))
logger.addHandler(console)
return logger
def main() -> None:
logger = configure()
logger.debug("not shown: the handler's level is INFO")
logger.info("processing %d items", 3) # lazy formatting
logger.warning("disk at %d%%", 91)
logger.error("failed to reach %s", "db.internal")
try:
1 / 0
except ZeroDivisionError:
logger.exception("computation failed") # ERROR + traceback
# Loggers are hierarchical: "demo.db" inherits demo's handlers.
child = logging.getLogger("demo.db")
child.info("child logger propagates to the parent's handler")
print(f"child level inherited: {child.getEffectiveLevel()} "
f"(= {logging.getLevelName(child.getEffectiveLevel())})")
# Per-call context without a filter.
logger.info("with extra", extra={"request_id": "req-99"})
# Levels are just integers, so comparisons work as expected.
print(f"levels: DEBUG={logging.DEBUG} INFO={logging.INFO} "
f"WARNING={logging.WARNING} ERROR={logging.ERROR} CRITICAL={logging.CRITICAL}")
print("libraries: getLogger(__name__) and nothing else; apps configure handlers")
if __name__ == "__main__":
main()