Skip to content

Commit

Permalink
Add option to enable debug mode in Log4j2 integration. (#1492)
Browse files Browse the repository at this point in the history
  • Loading branch information
maciejwalkowiak committed May 27, 2021
1 parent 730e7d5 commit d58fbdb
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Unreleased

* Feat: Add option to enable debug mode in Log4j2 integration (#1492)

# 5.0.0-beta.5

* Feat: OkHttp callback for Customising the Span (#1478)
Expand Down
4 changes: 2 additions & 2 deletions sentry-log4j2/api/sentry-log4j2.api
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ public final class io/sentry/log4j2/BuildConfig {
}

public final class io/sentry/log4j2/SentryAppender : org/apache/logging/log4j/core/appender/AbstractAppender {
public fun <init> (Ljava/lang/String;Lorg/apache/logging/log4j/core/Filter;Ljava/lang/String;Lorg/apache/logging/log4j/Level;Lorg/apache/logging/log4j/Level;Lio/sentry/ITransportFactory;Lio/sentry/IHub;)V
public fun <init> (Ljava/lang/String;Lorg/apache/logging/log4j/core/Filter;Ljava/lang/String;Lorg/apache/logging/log4j/Level;Lorg/apache/logging/log4j/Level;Ljava/lang/Boolean;Lio/sentry/ITransportFactory;Lio/sentry/IHub;)V
public fun append (Lorg/apache/logging/log4j/core/LogEvent;)V
public static fun createAppender (Ljava/lang/String;Lorg/apache/logging/log4j/Level;Lorg/apache/logging/log4j/Level;Ljava/lang/String;Lorg/apache/logging/log4j/core/Filter;)Lio/sentry/log4j2/SentryAppender;
public static fun createAppender (Ljava/lang/String;Lorg/apache/logging/log4j/Level;Lorg/apache/logging/log4j/Level;Ljava/lang/String;Ljava/lang/Boolean;Lorg/apache/logging/log4j/core/Filter;)Lio/sentry/log4j2/SentryAppender;
public fun start ()V
}

Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public final class SentryAppender extends AbstractAppender {
private final @Nullable ITransportFactory transportFactory;
private @NotNull Level minimumBreadcrumbLevel = Level.INFO;
private @NotNull Level minimumEventLevel = Level.ERROR;
private final @Nullable Boolean debug;
private final @NotNull IHub hub;

public SentryAppender(
Expand All @@ -46,6 +47,7 @@ public SentryAppender(
final @Nullable String dsn,
final @Nullable Level minimumBreadcrumbLevel,
final @Nullable Level minimumEventLevel,
final @Nullable Boolean debug,
final @Nullable ITransportFactory transportFactory,
final @NotNull IHub hub) {
super(name, filter, null, true, null);
Expand All @@ -56,6 +58,7 @@ public SentryAppender(
if (minimumEventLevel != null) {
this.minimumEventLevel = minimumEventLevel;
}
this.debug = debug;
this.transportFactory = transportFactory;
this.hub = hub;
}
Expand All @@ -67,6 +70,7 @@ public SentryAppender(
* @param minimumBreadcrumbLevel The min. level of the breadcrumb.
* @param minimumEventLevel The min. level of the event.
* @param dsn the Sentry DSN.
* @param debug if Sentry debug mode should be on
* @param filter The filter, if any, to use.
* @return The SentryAppender.
*/
Expand All @@ -76,6 +80,7 @@ public SentryAppender(
@Nullable @PluginAttribute("minimumBreadcrumbLevel") final Level minimumBreadcrumbLevel,
@Nullable @PluginAttribute("minimumEventLevel") final Level minimumEventLevel,
@Nullable @PluginAttribute("dsn") final String dsn,
@Nullable @PluginAttribute("debug") final Boolean debug,
@Nullable @PluginElement("filter") final Filter filter) {

if (name == null) {
Expand All @@ -88,6 +93,7 @@ public SentryAppender(
dsn,
minimumBreadcrumbLevel,
minimumEventLevel,
debug,
null,
HubAdapter.getInstance());
}
Expand All @@ -100,6 +106,7 @@ public void start() {
options -> {
options.setEnableExternalConfiguration(true);
options.setDsn(dsn);
options.setDebug(debug);
options.setSentryClientName(BuildConfig.SENTRY_LOG4J2_SDK_NAME);
options.setSdkVersion(createSdkVersion(options));
Optional.ofNullable(transportFactory).ifPresent(options::setTransportFactory);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ class SentryAppenderTest {
whenever(transportFactory.create(any(), any())).thenReturn(transport)
}

fun getSut(transportFactory: ITransportFactory? = null, minimumBreadcrumbLevel: Level? = null, minimumEventLevel: Level? = null): ExtendedLogger {
fun getSut(transportFactory: ITransportFactory? = null, minimumBreadcrumbLevel: Level? = null, minimumEventLevel: Level? = null, debug: Boolean? = null): ExtendedLogger {
if (transportFactory != null) {
this.transportFactory = transportFactory
}
loggerContext.start()
val config: Configuration = loggerContext.configuration
val appender = SentryAppender("sentry", null, "http://key@localhost/proj", minimumBreadcrumbLevel, minimumEventLevel, this.transportFactory, HubAdapter.getInstance())
val appender = SentryAppender("sentry", null, "http://key@localhost/proj", minimumBreadcrumbLevel, minimumEventLevel, debug, this.transportFactory, HubAdapter.getInstance())
config.addAppender(appender)

val ref = AppenderRef.createAppenderRef("sentry", null, null)
Expand Down Expand Up @@ -370,4 +370,10 @@ class SentryAppenderTest {
}, anyOrNull())
}
}

@Test
fun `sets the debug mode`() {
fixture.getSut(debug = true)
assertTrue(HubAdapter.getInstance().options.isDebug)
}
}

0 comments on commit d58fbdb

Please sign in to comment.