2626import io .sentry .protocol .SdkVersion ;
2727import io .sentry .util .CollectionUtils ;
2828import io .sentry .util .ContextTagsUtil ;
29-
3029import java .util .Arrays ;
3130import java .util .Collections ;
3231import java .util .List ;
3332import java .util .Map ;
3433import java .util .Objects ;
3534import java .util .Optional ;
3635import java .util .stream .Collectors ;
37-
3836import org .apache .logging .log4j .Level ;
3937import org .apache .logging .log4j .core .Filter ;
4038import org .apache .logging .log4j .core .LogEvent ;
4745import org .jetbrains .annotations .NotNull ;
4846import org .jetbrains .annotations .Nullable ;
4947
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. */
5349@ Plugin (name = "Sentry" , category = "Core" , elementType = "appender" , printObject = true )
5450@ Open
5551public class SentryAppender extends AbstractAppender {
@@ -66,49 +62,49 @@ public class SentryAppender extends AbstractAppender {
6662
6763 static {
6864 SentryIntegrationPackageStorage .getInstance ()
69- .addPackage ("maven:io.sentry:sentry-log4j2" , BuildConfig .VERSION_NAME );
65+ .addPackage ("maven:io.sentry:sentry-log4j2" , BuildConfig .VERSION_NAME );
7066 }
7167
7268 /**
7369 * @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.
7571 */
7672 @ Deprecated
7773 @ SuppressWarnings ("InlineMeSuggester" )
7874 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 ) {
8884 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 );
9995 }
10096
10197 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 ) {
112108 super (name , filter , null , true , null );
113109 this .dsn = dsn ;
114110 if (minimumBreadcrumbLevel != null ) {
@@ -129,64 +125,64 @@ public SentryAppender(
129125 /**
130126 * Create a Sentry Appender.
131127 *
132- * @param name The name of the Appender.
128+ * @param name The name of the Appender.
133129 * @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.
139135 * @return The SentryAppender.
140136 */
141137 @ PluginFactory
142138 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 ) {
151147
152148 if (name == null ) {
153149 LOGGER .error ("No name provided for SentryAppender" );
154150 return null ;
155151 }
156152 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 );
167163 }
168164
169165 @ Override
170166 public void start () {
171167 try {
172168 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+ }
186183 }
187- }
188- Optional .ofNullable (transportFactory ).ifPresent (options ::setTransportFactory );
189- });
184+ Optional .ofNullable (transportFactory ).ifPresent (options ::setTransportFactory );
185+ });
190186 } catch (IllegalArgumentException e ) {
191187 LOGGER .warn ("Failed to init Sentry during appender initialization: " + e .getMessage ());
192188 }
@@ -197,7 +193,7 @@ public void start() {
197193 @ Override
198194 public void append (final @ NotNull LogEvent eventObject ) {
199195 if (scopes .getOptions ().getLogs ().isEnabled ()
200- && eventObject .getLevel ().isMoreSpecificThan (minimumLevel )) {
196+ && eventObject .getLevel ().isMoreSpecificThan (minimumLevel )) {
201197 captureLog (eventObject );
202198 }
203199 if (eventObject .getLevel ().isMoreSpecificThan (minimumEventLevel )) {
@@ -232,7 +228,7 @@ protected void captureLog(@NotNull LogEvent loggingEvent) {
232228
233229 if (nonFormattedMessage != null && !formattedMessage .equals (nonFormattedMessage )) {
234230 attributes .add (
235- SentryAttribute .stringAttribute ("sentry.message.template" , nonFormattedMessage ));
231+ SentryAttribute .stringAttribute ("sentry.message.template" , nonFormattedMessage ));
236232 }
237233
238234 final Map <String , String > contextData = loggingEvent .getContextData ().toMap ();
@@ -269,8 +265,8 @@ protected void captureLog(@NotNull LogEvent loggingEvent) {
269265 final Mechanism mechanism = new Mechanism ();
270266 mechanism .setType (MECHANISM_TYPE );
271267 final Throwable mechanismException =
272- new ExceptionMechanismException (
273- mechanism , throwableInformation .getThrowable (), Thread .currentThread ());
268+ new ExceptionMechanismException (
269+ mechanism , throwableInformation .getThrowable (), Thread .currentThread ());
274270 event .setThrowable (mechanismException );
275271 }
276272
@@ -283,8 +279,8 @@ protected void captureLog(@NotNull LogEvent loggingEvent) {
283279 }
284280
285281 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 );
288284 if (!contextData .isEmpty ()) {
289285 // get tags from ScopesAdapter options to allow getting the correct tags if Sentry has been
290286 // initialized somewhere else
@@ -304,9 +300,9 @@ protected void captureLog(@NotNull LogEvent loggingEvent) {
304300 private @ NotNull List <String > toParams (final @ Nullable Object [] arguments ) {
305301 if (arguments != null ) {
306302 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 ());
310306 } else {
311307 return Collections .emptyList ();
312308 }
0 commit comments