26
26
import io .sentry .protocol .SdkVersion ;
27
27
import io .sentry .util .CollectionUtils ;
28
28
import io .sentry .util .ContextTagsUtil ;
29
-
30
29
import java .util .Arrays ;
31
30
import java .util .Collections ;
32
31
import java .util .List ;
33
32
import java .util .Map ;
34
33
import java .util .Objects ;
35
34
import java .util .Optional ;
36
35
import java .util .stream .Collectors ;
37
-
38
36
import org .apache .logging .log4j .Level ;
39
37
import org .apache .logging .log4j .core .Filter ;
40
38
import org .apache .logging .log4j .core .LogEvent ;
47
45
import org .jetbrains .annotations .NotNull ;
48
46
import org .jetbrains .annotations .Nullable ;
49
47
50
- /**
51
- * Appender for Log4j2 in charge of sending the logged events to a Sentry server.
52
- */
48
+ /** Appender for Log4j2 in charge of sending the logged events to a Sentry server. */
53
49
@ Plugin (name = "Sentry" , category = "Core" , elementType = "appender" , printObject = true )
54
50
@ Open
55
51
public class SentryAppender extends AbstractAppender {
@@ -66,49 +62,49 @@ public class SentryAppender extends AbstractAppender {
66
62
67
63
static {
68
64
SentryIntegrationPackageStorage .getInstance ()
69
- .addPackage ("maven:io.sentry:sentry-log4j2" , BuildConfig .VERSION_NAME );
65
+ .addPackage ("maven:io.sentry:sentry-log4j2" , BuildConfig .VERSION_NAME );
70
66
}
71
67
72
68
/**
73
69
* @deprecated This constructor is deprecated. Please use {@link #SentryAppender(String, Filter,
74
- * String, Level, Level, Level, Boolean, ITransportFactory, IScopes, String[])} instead.
70
+ * String, Level, Level, Level, Boolean, ITransportFactory, IScopes, String[])} instead.
75
71
*/
76
72
@ Deprecated
77
73
@ SuppressWarnings ("InlineMeSuggester" )
78
74
public SentryAppender (
79
- final @ NotNull String name ,
80
- final @ Nullable Filter filter ,
81
- final @ Nullable String dsn ,
82
- final @ Nullable Level minimumBreadcrumbLevel ,
83
- final @ Nullable Level minimumEventLevel ,
84
- final @ Nullable Boolean debug ,
85
- final @ Nullable ITransportFactory transportFactory ,
86
- final @ NotNull IScopes scopes ,
87
- final @ Nullable String [] contextTags ) {
75
+ final @ NotNull String name ,
76
+ final @ Nullable Filter filter ,
77
+ final @ Nullable String dsn ,
78
+ final @ Nullable Level minimumBreadcrumbLevel ,
79
+ final @ Nullable Level minimumEventLevel ,
80
+ final @ Nullable Boolean debug ,
81
+ final @ Nullable ITransportFactory transportFactory ,
82
+ final @ NotNull IScopes scopes ,
83
+ final @ Nullable String [] contextTags ) {
88
84
this (
89
- name ,
90
- filter ,
91
- dsn ,
92
- minimumBreadcrumbLevel ,
93
- minimumEventLevel ,
94
- null ,
95
- debug ,
96
- transportFactory ,
97
- scopes ,
98
- contextTags );
85
+ name ,
86
+ filter ,
87
+ dsn ,
88
+ minimumBreadcrumbLevel ,
89
+ minimumEventLevel ,
90
+ null ,
91
+ debug ,
92
+ transportFactory ,
93
+ scopes ,
94
+ contextTags );
99
95
}
100
96
101
97
public SentryAppender (
102
- final @ NotNull String name ,
103
- final @ Nullable Filter filter ,
104
- final @ Nullable String dsn ,
105
- final @ Nullable Level minimumBreadcrumbLevel ,
106
- final @ Nullable Level minimumEventLevel ,
107
- final @ Nullable Level minimumLevel ,
108
- final @ Nullable Boolean debug ,
109
- final @ Nullable ITransportFactory transportFactory ,
110
- final @ NotNull IScopes scopes ,
111
- final @ Nullable String [] contextTags ) {
98
+ final @ NotNull String name ,
99
+ final @ Nullable Filter filter ,
100
+ final @ Nullable String dsn ,
101
+ final @ Nullable Level minimumBreadcrumbLevel ,
102
+ final @ Nullable Level minimumEventLevel ,
103
+ final @ Nullable Level minimumLevel ,
104
+ final @ Nullable Boolean debug ,
105
+ final @ Nullable ITransportFactory transportFactory ,
106
+ final @ NotNull IScopes scopes ,
107
+ final @ Nullable String [] contextTags ) {
112
108
super (name , filter , null , true , null );
113
109
this .dsn = dsn ;
114
110
if (minimumBreadcrumbLevel != null ) {
@@ -129,64 +125,64 @@ public SentryAppender(
129
125
/**
130
126
* Create a Sentry Appender.
131
127
*
132
- * @param name The name of the Appender.
128
+ * @param name The name of the Appender.
133
129
* @param minimumBreadcrumbLevel The min. level of the breadcrumb.
134
- * @param minimumEventLevel The min. level of the event.
135
- * @param minimumLevel The min. level of the log event.
136
- * @param dsn the Sentry DSN.
137
- * @param debug if Sentry debug mode should be on
138
- * @param filter The filter, if any, to use.
130
+ * @param minimumEventLevel The min. level of the event.
131
+ * @param minimumLevel The min. level of the log event.
132
+ * @param dsn the Sentry DSN.
133
+ * @param debug if Sentry debug mode should be on
134
+ * @param filter The filter, if any, to use.
139
135
* @return The SentryAppender.
140
136
*/
141
137
@ PluginFactory
142
138
public static @ Nullable SentryAppender createAppender (
143
- @ Nullable @ PluginAttribute ("name" ) final String name ,
144
- @ Nullable @ PluginAttribute ("minimumBreadcrumbLevel" ) final Level minimumBreadcrumbLevel ,
145
- @ Nullable @ PluginAttribute ("minimumEventLevel" ) final Level minimumEventLevel ,
146
- @ Nullable @ PluginAttribute ("minimumLevel" ) final Level minimumLevel ,
147
- @ Nullable @ PluginAttribute ("dsn" ) final String dsn ,
148
- @ Nullable @ PluginAttribute ("debug" ) final Boolean debug ,
149
- @ Nullable @ PluginElement ("filter" ) final Filter filter ,
150
- @ Nullable @ PluginAttribute ("contextTags" ) final String contextTags ) {
139
+ @ Nullable @ PluginAttribute ("name" ) final String name ,
140
+ @ Nullable @ PluginAttribute ("minimumBreadcrumbLevel" ) final Level minimumBreadcrumbLevel ,
141
+ @ Nullable @ PluginAttribute ("minimumEventLevel" ) final Level minimumEventLevel ,
142
+ @ Nullable @ PluginAttribute ("minimumLevel" ) final Level minimumLevel ,
143
+ @ Nullable @ PluginAttribute ("dsn" ) final String dsn ,
144
+ @ Nullable @ PluginAttribute ("debug" ) final Boolean debug ,
145
+ @ Nullable @ PluginElement ("filter" ) final Filter filter ,
146
+ @ Nullable @ PluginAttribute ("contextTags" ) final String contextTags ) {
151
147
152
148
if (name == null ) {
153
149
LOGGER .error ("No name provided for SentryAppender" );
154
150
return null ;
155
151
}
156
152
return new SentryAppender (
157
- name ,
158
- filter ,
159
- dsn ,
160
- minimumBreadcrumbLevel ,
161
- minimumEventLevel ,
162
- minimumLevel ,
163
- debug ,
164
- null ,
165
- ScopesAdapter .getInstance (),
166
- contextTags != null ? contextTags .split ("," ) : null );
153
+ name ,
154
+ filter ,
155
+ dsn ,
156
+ minimumBreadcrumbLevel ,
157
+ minimumEventLevel ,
158
+ minimumLevel ,
159
+ debug ,
160
+ null ,
161
+ ScopesAdapter .getInstance (),
162
+ contextTags != null ? contextTags .split ("," ) : null );
167
163
}
168
164
169
165
@ Override
170
166
public void start () {
171
167
try {
172
168
Sentry .init (
173
- options -> {
174
- options .setEnableExternalConfiguration (true );
175
- options .setInitPriority (InitPriority .LOWEST );
176
- options .setDsn (dsn );
177
- if (debug != null ) {
178
- options .setDebug (debug );
179
- }
180
- options .setSentryClientName (
181
- BuildConfig .SENTRY_LOG4J2_SDK_NAME + "/" + BuildConfig .VERSION_NAME );
182
- options .setSdkVersion (createSdkVersion (options ));
183
- if (contextTags != null ) {
184
- for (final String contextTag : contextTags ) {
185
- options .addContextTag (contextTag );
169
+ options -> {
170
+ options .setEnableExternalConfiguration (true );
171
+ options .setInitPriority (InitPriority .LOWEST );
172
+ options .setDsn (dsn );
173
+ if (debug != null ) {
174
+ options .setDebug (debug );
175
+ }
176
+ options .setSentryClientName (
177
+ BuildConfig .SENTRY_LOG4J2_SDK_NAME + "/" + BuildConfig .VERSION_NAME );
178
+ options .setSdkVersion (createSdkVersion (options ));
179
+ if (contextTags != null ) {
180
+ for (final String contextTag : contextTags ) {
181
+ options .addContextTag (contextTag );
182
+ }
186
183
}
187
- }
188
- Optional .ofNullable (transportFactory ).ifPresent (options ::setTransportFactory );
189
- });
184
+ Optional .ofNullable (transportFactory ).ifPresent (options ::setTransportFactory );
185
+ });
190
186
} catch (IllegalArgumentException e ) {
191
187
LOGGER .warn ("Failed to init Sentry during appender initialization: " + e .getMessage ());
192
188
}
@@ -197,7 +193,7 @@ public void start() {
197
193
@ Override
198
194
public void append (final @ NotNull LogEvent eventObject ) {
199
195
if (scopes .getOptions ().getLogs ().isEnabled ()
200
- && eventObject .getLevel ().isMoreSpecificThan (minimumLevel )) {
196
+ && eventObject .getLevel ().isMoreSpecificThan (minimumLevel )) {
201
197
captureLog (eventObject );
202
198
}
203
199
if (eventObject .getLevel ().isMoreSpecificThan (minimumEventLevel )) {
@@ -232,7 +228,7 @@ protected void captureLog(@NotNull LogEvent loggingEvent) {
232
228
233
229
if (nonFormattedMessage != null && !formattedMessage .equals (nonFormattedMessage )) {
234
230
attributes .add (
235
- SentryAttribute .stringAttribute ("sentry.message.template" , nonFormattedMessage ));
231
+ SentryAttribute .stringAttribute ("sentry.message.template" , nonFormattedMessage ));
236
232
}
237
233
238
234
final Map <String , String > contextData = loggingEvent .getContextData ().toMap ();
@@ -269,8 +265,8 @@ protected void captureLog(@NotNull LogEvent loggingEvent) {
269
265
final Mechanism mechanism = new Mechanism ();
270
266
mechanism .setType (MECHANISM_TYPE );
271
267
final Throwable mechanismException =
272
- new ExceptionMechanismException (
273
- mechanism , throwableInformation .getThrowable (), Thread .currentThread ());
268
+ new ExceptionMechanismException (
269
+ mechanism , throwableInformation .getThrowable (), Thread .currentThread ());
274
270
event .setThrowable (mechanismException );
275
271
}
276
272
@@ -283,8 +279,8 @@ protected void captureLog(@NotNull LogEvent loggingEvent) {
283
279
}
284
280
285
281
final Map <String , String > contextData =
286
- CollectionUtils .filterMapEntries (
287
- loggingEvent .getContextData ().toMap (), entry -> entry .getValue () != null );
282
+ CollectionUtils .filterMapEntries (
283
+ loggingEvent .getContextData ().toMap (), entry -> entry .getValue () != null );
288
284
if (!contextData .isEmpty ()) {
289
285
// get tags from ScopesAdapter options to allow getting the correct tags if Sentry has been
290
286
// initialized somewhere else
@@ -304,9 +300,9 @@ protected void captureLog(@NotNull LogEvent loggingEvent) {
304
300
private @ NotNull List <String > toParams (final @ Nullable Object [] arguments ) {
305
301
if (arguments != null ) {
306
302
return Arrays .stream (arguments )
307
- .filter (Objects ::nonNull )
308
- .map (Object ::toString )
309
- .collect (Collectors .toList ());
303
+ .filter (Objects ::nonNull )
304
+ .map (Object ::toString )
305
+ .collect (Collectors .toList ());
310
306
} else {
311
307
return Collections .emptyList ();
312
308
}
0 commit comments