-
-
Notifications
You must be signed in to change notification settings - Fork 468
Expand file tree
/
Copy pathSentryHandler.java
More file actions
350 lines (315 loc) · 11.6 KB
/
SentryHandler.java
File metadata and controls
350 lines (315 loc) · 11.6 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
package io.sentry.jul;
import static io.sentry.TypeCheckHint.JUL_LOG_RECORD;
import static io.sentry.TypeCheckHint.SENTRY_SYNTHETIC_EXCEPTION;
import com.jakewharton.nopen.annotation.Open;
import io.sentry.Breadcrumb;
import io.sentry.Hint;
import io.sentry.InitPriority;
import io.sentry.ScopesAdapter;
import io.sentry.Sentry;
import io.sentry.SentryEvent;
import io.sentry.SentryIntegrationPackageStorage;
import io.sentry.SentryLevel;
import io.sentry.SentryOptions;
import io.sentry.exception.ExceptionMechanismException;
import io.sentry.protocol.Mechanism;
import io.sentry.protocol.Message;
import io.sentry.protocol.SdkVersion;
import io.sentry.util.CollectionUtils;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.logging.ErrorManager;
import java.util.logging.Filter;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.LogRecord;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.TestOnly;
import org.slf4j.MDC;
/** Logging handler in charge of sending the java.util.logging records to a Sentry server. */
@Open
public class SentryHandler extends Handler {
public static final String MECHANISM_TYPE = "JulSentryHandler";
/** Name of the {@link SentryEvent} extra property containing the Thread id. */
public static final String THREAD_ID = "thread_id";
/**
* If true, <code>String.format()</code> is used to render parameterized log messages instead of
* <code>MessageFormat.format()</code>; Defaults to false.
*/
private boolean printfStyle;
private @NotNull Level minimumBreadcrumbLevel = Level.INFO;
private @NotNull Level minimumEventLevel = Level.SEVERE;
/** Creates an instance of SentryHandler. */
public SentryHandler() {
this(new SentryOptions());
}
/**
* Creates an instance of SentryHandler.
*
* @param options the SentryOptions
*/
public SentryHandler(final @NotNull SentryOptions options) {
this(options, true, true);
}
/**
* Creates an instance of SentryHandler.
*
* @param options the SentryOptions
* @param enableExternalConfiguration whether external options like sentry.properties and ENV vars
* should be parsed
*/
public SentryHandler(
final @NotNull SentryOptions options, final boolean enableExternalConfiguration) {
this(options, true, enableExternalConfiguration);
}
/** Creates an instance of SentryHandler. */
@TestOnly
SentryHandler(
final @NotNull SentryOptions options,
final boolean configureFromLogManager,
final boolean enableExternalConfiguration) {
setFilter(new DropSentryFilter());
if (configureFromLogManager) {
retrieveProperties();
}
options.setEnableExternalConfiguration(enableExternalConfiguration);
options.setInitPriority(InitPriority.LOWEST);
options.setSdkVersion(createSdkVersion(options));
Sentry.init(options);
addPackageAndIntegrationInfo();
}
@Override
public void publish(final @NotNull LogRecord record) {
// Do not log the event if the current thread is managed by sentry
if (!isLoggable(record)) {
return;
}
try {
if (record.getLevel().intValue() >= minimumEventLevel.intValue()) {
final Hint hint = new Hint();
hint.set(SENTRY_SYNTHETIC_EXCEPTION, record);
Sentry.captureEvent(createEvent(record), hint);
}
if (record.getLevel().intValue() >= minimumBreadcrumbLevel.intValue()) {
final Hint hint = new Hint();
hint.set(JUL_LOG_RECORD, record);
Sentry.addBreadcrumb(createBreadcrumb(record), hint);
}
} catch (RuntimeException e) {
reportError(
"An exception occurred while creating a new event in Sentry",
e,
ErrorManager.WRITE_FAILURE);
}
}
/** Retrieves the properties of the logger. */
private void retrieveProperties() {
final LogManager manager = LogManager.getLogManager();
final String className = SentryHandler.class.getName();
setPrintfStyle(Boolean.parseBoolean(manager.getProperty(className + ".printfStyle")));
setLevel(parseLevelOrDefault(manager.getProperty(className + ".level")));
final String minimumBreadCrumbLevel =
manager.getProperty(className + ".minimumBreadcrumbLevel");
if (minimumBreadCrumbLevel != null) {
setMinimumBreadcrumbLevel(parseLevelOrDefault(minimumBreadCrumbLevel));
}
final String minimumEventLevel = manager.getProperty(className + ".minimumEventLevel");
if (minimumEventLevel != null) {
setMinimumEventLevel(parseLevelOrDefault(minimumEventLevel));
}
}
/**
* Transforms a {@link Level} into an {@link SentryLevel}.
*
* @param level original level as defined in JUL.
* @return log level used within sentry.
*/
private static @Nullable SentryLevel formatLevel(final @NotNull Level level) {
if (level.intValue() >= Level.SEVERE.intValue()) {
return SentryLevel.ERROR;
} else if (level.intValue() >= Level.WARNING.intValue()) {
return SentryLevel.WARNING;
} else if (level.intValue() >= Level.INFO.intValue()) {
return SentryLevel.INFO;
} else if (level.intValue() >= Level.ALL.intValue()) {
return SentryLevel.DEBUG;
} else {
return null;
}
}
private @NotNull Level parseLevelOrDefault(final @NotNull String levelName) {
try {
return Level.parse(levelName.trim());
} catch (RuntimeException e) {
return Level.WARNING;
}
}
private @NotNull Breadcrumb createBreadcrumb(final @NotNull LogRecord record) {
final Breadcrumb breadcrumb = new Breadcrumb();
breadcrumb.setLevel(formatLevel(record.getLevel()));
breadcrumb.setCategory(record.getLoggerName());
if (record.getParameters() != null) {
try {
breadcrumb.setMessage(formatMessage(record.getMessage(), record.getParameters()));
} catch (RuntimeException e) {
breadcrumb.setMessage(record.getMessage());
}
} else {
breadcrumb.setMessage(record.getMessage());
}
return breadcrumb;
}
/**
* Creates {@link SentryEvent} from JUL's {@link LogRecord}.
*
* @param record the log record
* @return the sentry event
*/
// for the Android compatibility we must use old Java Date class
@SuppressWarnings({"JdkObsolete", "JavaUtilDate", "deprecation"})
@NotNull
SentryEvent createEvent(final @NotNull LogRecord record) {
final SentryEvent event = new SentryEvent(new Date(record.getMillis()));
event.setLevel(formatLevel(record.getLevel()));
event.setLogger(record.getLoggerName());
final Message sentryMessage = new Message();
sentryMessage.setParams(toParams(record.getParameters()));
String message = record.getMessage();
if (record.getResourceBundle() != null
&& record.getResourceBundle().containsKey(record.getMessage())) {
message = record.getResourceBundle().getString(record.getMessage());
}
sentryMessage.setMessage(message);
if (record.getParameters() != null) {
try {
sentryMessage.setFormatted(formatMessage(message, record.getParameters()));
} catch (RuntimeException e) {
// local formatting failed, send message and parameters without formatted string
}
}
event.setMessage(sentryMessage);
final Throwable throwable = record.getThrown();
if (throwable != null) {
final Mechanism mechanism = new Mechanism();
mechanism.setType(MECHANISM_TYPE);
final Throwable mechanismException =
new ExceptionMechanismException(mechanism, throwable, Thread.currentThread());
event.setThrowable(mechanismException);
}
Map<String, String> mdcProperties = MDC.getMDCAdapter().getCopyOfContextMap();
if (mdcProperties != null) {
mdcProperties =
CollectionUtils.filterMapEntries(mdcProperties, entry -> entry.getValue() != null);
if (!mdcProperties.isEmpty()) {
// get tags from ScopesAdapter options to allow getting the correct tags if Sentry has been
// initialized somewhere else
final List<String> contextTags = ScopesAdapter.getInstance().getOptions().getContextTags();
if (!contextTags.isEmpty()) {
for (final String contextTag : contextTags) {
// if mdc tag is listed in SentryOptions, apply as event tag
if (mdcProperties.containsKey(contextTag)) {
event.setTag(contextTag, mdcProperties.get(contextTag));
// remove from all tags applied to logging event
mdcProperties.remove(contextTag);
}
}
}
// put the rest of mdc tags in contexts
if (!mdcProperties.isEmpty()) {
event.getContexts().put("MDC", mdcProperties);
}
}
}
event.setExtra(THREAD_ID, record.getThreadID());
return event;
}
private @NotNull List<String> toParams(final @Nullable Object[] arguments) {
final List<String> result = new ArrayList<>();
if (arguments != null) {
for (Object argument : arguments) {
if (argument != null) {
result.add(argument.toString());
}
}
}
return result;
}
/**
* Returns formatted Event message when provided the message template and parameters.
*
* @param message Message template body.
* @param parameters Array of parameters for the message.
* @return Formatted message.
*/
private @NotNull String formatMessage(
final @NotNull String message, final @Nullable Object[] parameters) {
String formatted;
if (printfStyle) {
formatted = String.format(message, parameters);
} else {
formatted = MessageFormat.format(message, parameters);
}
return formatted;
}
@Override
public void flush() {}
@Override
public void close() throws SecurityException {
try {
Sentry.close();
} catch (RuntimeException e) {
reportError(
"An exception occurred while closing the Sentry connection",
e,
ErrorManager.CLOSE_FAILURE);
}
}
private @NotNull SdkVersion createSdkVersion(final @NotNull SentryOptions sentryOptions) {
SdkVersion sdkVersion = sentryOptions.getSdkVersion();
final String name = BuildConfig.SENTRY_JUL_SDK_NAME;
final String version = BuildConfig.VERSION_NAME;
sdkVersion = SdkVersion.updateSdkVersion(sdkVersion, name, version);
return sdkVersion;
}
private void addPackageAndIntegrationInfo() {
SentryIntegrationPackageStorage.getInstance()
.addPackage("maven:io.sentry:sentry-jul", BuildConfig.VERSION_NAME);
SentryIntegrationPackageStorage.getInstance().addIntegration("Jul");
}
public void setPrintfStyle(final boolean printfStyle) {
this.printfStyle = printfStyle;
}
public void setMinimumBreadcrumbLevel(final @Nullable Level minimumBreadcrumbLevel) {
if (minimumBreadcrumbLevel != null) {
this.minimumBreadcrumbLevel = minimumBreadcrumbLevel;
}
}
public @NotNull Level getMinimumBreadcrumbLevel() {
return minimumBreadcrumbLevel;
}
public void setMinimumEventLevel(final @Nullable Level minimumEventLevel) {
if (minimumEventLevel != null) {
this.minimumEventLevel = minimumEventLevel;
}
}
public @NotNull Level getMinimumEventLevel() {
return minimumEventLevel;
}
public boolean isPrintfStyle() {
return printfStyle;
}
private static final class DropSentryFilter implements Filter {
@Override
public boolean isLoggable(final @NotNull LogRecord record) {
final String loggerName = record.getLoggerName();
return loggerName == null
|| !loggerName.startsWith("io.sentry")
|| loggerName.startsWith("io.sentry.samples");
}
}
}