forked from mozilla/code-coverage
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog.py
More file actions
169 lines (138 loc) · 5.12 KB
/
Copy pathlog.py
File metadata and controls
169 lines (138 loc) · 5.12 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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# -*- coding: utf-8 -*-
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import logging
import logging.handlers
import os
import sys
import structlog
import importlib.metadata
import sentry_sdk
from sentry_sdk.integrations.logging import LoggingIntegration
root = logging.getLogger()
class AppNameFilter(logging.Filter):
def __init__(self, project_name, channel, *args, **kwargs):
self.project_name = project_name
self.channel = channel
super().__init__(*args, **kwargs)
def filter(self, record):
record.app_name = f"code-coverage/{self.channel}/{self.project_name}"
return True
class ExtraFormatter(logging.Formatter):
def format(self, record):
log = super().format(record)
extra = {
key: value
for key, value in record.__dict__.items()
if key
not in list(sentry_sdk.integrations.logging.COMMON_RECORD_ATTRS)
+ ["asctime", "app_name"]
}
if len(extra) > 0:
log += " | extra=" + str(extra)
return log
def setup_papertrail(project_name, channel, PAPERTRAIL_HOST, PAPERTRAIL_PORT):
"""
Setup papertrail account using taskcluster secrets
"""
# Setup papertrail
papertrail = logging.handlers.SysLogHandler(
address=(PAPERTRAIL_HOST, int(PAPERTRAIL_PORT)),
)
formatter = ExtraFormatter(
"%(app_name)s: %(asctime)s %(filename)s: %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
papertrail.setLevel(logging.INFO)
papertrail.setFormatter(formatter)
# This filter is used to add the 'app_name' value to all logs to be formatted
papertrail.addFilter(AppNameFilter(project_name, channel))
root.addHandler(papertrail)
def setup_sentry(name, channel, dsn):
"""
Setup sentry account using taskcluster secrets
"""
# Detect environment
task_id = os.environ.get("TASK_ID")
if task_id is not None:
site = "taskcluster"
elif "DYNO" in os.environ:
site = "heroku"
else:
site = "unknown"
# This integration allows sentry to catch logs from logging and process them
# By default, the 'event_level' is set to ERROR, we are defining it to WARNING
sentry_logging = LoggingIntegration(
level=logging.INFO, # Capture INFO and above as breadcrumbs
event_level=logging.WARNING, # Send WARNINGs as events
)
# sentry_sdk will automatically retrieve the 'extra' attribute from logs and
# add contained values as Additional Data on the dashboard of the Sentry issue
sentry_sdk.init(
dsn=dsn,
integrations=[sentry_logging],
server_name=name,
environment=channel,
release=importlib.metadata.version(f"code-coverage-{name}"),
)
sentry_sdk.set_tag("site", site)
if task_id is not None:
# Add a Taskcluster task id when available
# It will be shown in a new section called Task on the dashboard
sentry_sdk.set_context("task", {"task_id": task_id})
class RenameAttrsProcessor(structlog.processors.KeyValueRenderer):
"""
Rename event_dict keys that will attempt to overwrite LogRecord common
attributes during structlog.stdlib.render_to_log_kwargs processing
"""
def __call__(self, logger, method_name, event_dict):
to_rename = [
key
for key in event_dict
if key in sentry_sdk.integrations.logging.COMMON_RECORD_ATTRS
]
for key in to_rename:
event_dict[f"{key}_"] = event_dict[key]
event_dict.pop(key)
return event_dict
def init_logger(
project_name,
channel=None,
level=logging.INFO,
PAPERTRAIL_HOST=None,
PAPERTRAIL_PORT=None,
SENTRY_DSN=None,
):
if not channel:
channel = os.environ.get("APP_CHANNEL")
logging.basicConfig(
format="%(asctime)s.%(msecs)06d [%(levelname)-8s] %(filename)s: %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
stream=sys.stdout,
level=level,
)
# Log to papertrail
if channel and PAPERTRAIL_HOST and PAPERTRAIL_PORT:
setup_papertrail(project_name, channel, PAPERTRAIL_HOST, PAPERTRAIL_PORT)
# Log to sentry
if channel and SENTRY_DSN:
setup_sentry(project_name, channel, SENTRY_DSN)
# Setup structlog
processors = [
structlog.stdlib.PositionalArgumentsFormatter(),
structlog.processors.StackInfoRenderer(),
structlog.processors.format_exc_info,
RenameAttrsProcessor(),
# Transpose the 'event_dict' from structlog into keyword arguments for logging.log
# E.g.: 'event' become 'msg' and, at the end, all remaining values from 'event_dict'
# are added as 'extra'
structlog.stdlib.render_to_log_kwargs,
]
structlog.configure(
processors=processors,
context_class=structlog.threadlocal.wrap_dict(dict),
logger_factory=structlog.stdlib.LoggerFactory(),
wrapper_class=structlog.stdlib.BoundLogger,
cache_logger_on_first_use=True,
)