forked from mozilla-conduit/lando-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogging.py
147 lines (122 loc) · 4.33 KB
/
logging.py
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import json
import logging
import socket
import traceback
from landoapi.systems import Subsystem
logger = logging.getLogger(__name__)
class MozLogFormatter(logging.Formatter):
"""A mozlog logging formatter.
https://mzl.la/2NhT1E6
"""
MOZLOG_ENVVERSION = "2.0"
# Syslog severity levels.
SL_EMERG = 0 # system is unusable
SL_ALERT = 1 # action must be taken immediately
SL_CRIT = 2 # critical conditions
SL_ERR = 3 # error conditions
SL_WARNING = 4 # warning conditions
SL_NOTICE = 5 # normal but significant condition
SL_INFO = 6 # informational
SL_DEBUG = 7 # debug-level messages
# Mapping from python logging priority to Syslog severity level.
PRIORITY = {
"DEBUG": SL_DEBUG,
"INFO": SL_INFO,
"WARNING": SL_WARNING,
"ERROR": SL_ERR,
"CRITICAL": SL_CRIT,
}
BUILTIN_LOGRECORD_ATTRIBUTES = set(
(
"args",
"asctime",
"created",
"exc_info",
"exc_text",
"filename",
"funcName",
"levelname",
"levelno",
"lineno",
"module",
"msecs",
"message",
"msg",
"name",
"pathname",
"process",
"processName",
"relativeCreated",
"stack_info",
"thread",
"threadName",
)
)
def __init__(self, *args, mozlog_logger=None, **kwargs):
self.mozlog_logger = mozlog_logger or "Dockerflow"
self.hostname = socket.gethostname()
super().__init__(*args, **kwargs)
def format(self, record):
"""Formats a log record and serializes to mozlog json"""
mozlog_record = {
"EnvVersion": self.MOZLOG_ENVVERSION,
"Hostname": self.hostname,
"Logger": self.mozlog_logger,
"Type": record.name,
"Timestamp": int(record.created * 1e9),
"Severity": self.PRIORITY.get(record.levelname, self.SL_WARNING),
"Pid": record.process,
"Fields": {
k: v
for k, v in record.__dict__.items()
if k not in self.BUILTIN_LOGRECORD_ATTRIBUTES
},
}
msg = record.getMessage()
if msg and "msg" not in mozlog_record["Fields"]:
mozlog_record["Fields"]["msg"] = msg
if record.exc_info is not None:
mozlog_record["Fields"]["exc"] = {
"error": repr(record.exc_info[1]), # Instance
"traceback": "".join(traceback.format_tb(record.exc_info[2])),
}
return self.serialize(mozlog_record)
def serialize(self, mozlog_record):
"""Serialize a mozlog record."""
return json.dumps(mozlog_record, sort_keys=True)
class PrettyMozLogFormatter(MozLogFormatter):
"""A mozlog logging formatter which pretty prints."""
def serialize(self, mozlog_record):
"""Serialize a mozlog record."""
return json.dumps(mozlog_record, sort_keys=True, indent=2)
class LoggingSubsystem(Subsystem):
name = "logging"
def init_app(self, app):
self.flask_app = app
level = self.flask_app.config.get("LOG_LEVEL", "INFO")
logging.config.dictConfig(
{
"version": 1,
"formatters": {
"mozlog": {"()": MozLogFormatter, "mozlog_logger": "lando-api"}
},
"handlers": {
"console": {
"class": "logging.StreamHandler",
"formatter": "mozlog",
},
"null": {"class": "logging.NullHandler"},
},
"loggers": {
"landoapi": {"level": level, "handlers": ["console"]},
"request.summary": {"level": level, "handlers": ["console"]},
"flask": {"handlers": ["null"]},
"werkzeug": {"level": "ERROR", "handlers": ["console"]},
"celery": {"level": "INFO", "handlers": ["console"]},
},
"root": {"handlers": ["null"]},
"disable_existing_loggers": True,
}
)
logger.info("logging configured", extra={"LOG_LEVEL": level})
logging_subsystem = LoggingSubsystem()