diff --git a/sentry-jul/src/test/kotlin/io/sentry/jul/SentryHandlerTest.kt b/sentry-jul/src/test/kotlin/io/sentry/jul/SentryHandlerTest.kt index e86f06a4ab7..ab160faac40 100644 --- a/sentry-jul/src/test/kotlin/io/sentry/jul/SentryHandlerTest.kt +++ b/sentry-jul/src/test/kotlin/io/sentry/jul/SentryHandlerTest.kt @@ -7,6 +7,9 @@ import io.sentry.SentryLogLevel import io.sentry.SentryOptions import io.sentry.checkEvent import io.sentry.checkLogs +import io.sentry.logger.ILoggerBatchProcessorFactory +import io.sentry.logger.LoggerBatchProcessor +import io.sentry.test.ImmediateExecutorService import io.sentry.test.applyTestOptions import io.sentry.test.initForTest import io.sentry.transport.ITransport @@ -45,6 +48,9 @@ class SentryHandlerTest { val options = SentryOptions() options.dsn = "http://key@localhost/proj" options.setTransportFactory { _, _ -> transport } + options.logs.loggerBatchProcessorFactory = ILoggerBatchProcessorFactory { options, client -> + LoggerBatchProcessor(options, client, ImmediateExecutorService()) + } applyTestOptions(options) contextTags?.forEach { options.addContextTag(it) } logger = Logger.getLogger("jul.SentryHandlerTest") diff --git a/sentry-log4j2/src/main/java/io/sentry/log4j2/SentryAppender.java b/sentry-log4j2/src/main/java/io/sentry/log4j2/SentryAppender.java index 6885cdb81fd..df0f9eeb2d2 100644 --- a/sentry-log4j2/src/main/java/io/sentry/log4j2/SentryAppender.java +++ b/sentry-log4j2/src/main/java/io/sentry/log4j2/SentryAppender.java @@ -164,25 +164,37 @@ public SentryAppender( @Override public void start() { + start(getOptionsConfiguration(null)); + } + + @NotNull + Sentry.OptionsConfiguration getOptionsConfiguration( + final @Nullable Sentry.OptionsConfiguration additionalOptionsConfiguration) { + return options -> { + options.setEnableExternalConfiguration(true); + options.setInitPriority(InitPriority.LOWEST); + options.setDsn(dsn); + if (debug != null) { + options.setDebug(debug); + } + options.setSentryClientName( + BuildConfig.SENTRY_LOG4J2_SDK_NAME + "/" + BuildConfig.VERSION_NAME); + options.setSdkVersion(createSdkVersion(options)); + if (contextTags != null) { + for (final String contextTag : contextTags) { + options.addContextTag(contextTag); + } + } + Optional.ofNullable(transportFactory).ifPresent(options::setTransportFactory); + if (additionalOptionsConfiguration != null) { + additionalOptionsConfiguration.configure(options); + } + }; + } + + void start(final @NotNull Sentry.OptionsConfiguration optionsConfiguration) { try { - Sentry.init( - options -> { - options.setEnableExternalConfiguration(true); - options.setInitPriority(InitPriority.LOWEST); - options.setDsn(dsn); - if (debug != null) { - options.setDebug(debug); - } - options.setSentryClientName( - BuildConfig.SENTRY_LOG4J2_SDK_NAME + "/" + BuildConfig.VERSION_NAME); - options.setSdkVersion(createSdkVersion(options)); - if (contextTags != null) { - for (final String contextTag : contextTags) { - options.addContextTag(contextTag); - } - } - Optional.ofNullable(transportFactory).ifPresent(options::setTransportFactory); - }); + Sentry.init(optionsConfiguration); } catch (IllegalArgumentException e) { final @Nullable String errorMessage = e.getMessage(); if (errorMessage == null || !errorMessage.startsWith("DSN is required.")) { @@ -235,14 +247,13 @@ protected void captureLog(@NotNull LogEvent loggingEvent) { } final @NotNull Map contextData = loggingEvent.getContextData().toMap(); - final @NotNull List contextTags = - ScopesAdapter.getInstance().getOptions().getContextTags(); + final @NotNull List contextTags = scopes.getOptions().getContextTags(); LoggerPropertiesUtil.applyPropertiesToAttributes(attributes, contextTags, contextData); final @NotNull SentryLogParameters params = SentryLogParameters.create(attributes); params.setOrigin("auto.log.log4j2"); - Sentry.logger().log(sentryLevel, params, formattedMessage, arguments); + scopes.logger().log(sentryLevel, params, formattedMessage, arguments); } /** diff --git a/sentry-log4j2/src/test/kotlin/io/sentry/log4j2/SentryAppenderTest.kt b/sentry-log4j2/src/test/kotlin/io/sentry/log4j2/SentryAppenderTest.kt index 9923f81f00f..c32459ea022 100644 --- a/sentry-log4j2/src/test/kotlin/io/sentry/log4j2/SentryAppenderTest.kt +++ b/sentry-log4j2/src/test/kotlin/io/sentry/log4j2/SentryAppenderTest.kt @@ -8,6 +8,9 @@ import io.sentry.SentryLevel import io.sentry.SentryLogLevel import io.sentry.checkEvent import io.sentry.checkLogs +import io.sentry.logger.ILoggerBatchProcessorFactory +import io.sentry.logger.LoggerBatchProcessor +import io.sentry.test.ImmediateExecutorService import io.sentry.test.initForTest import io.sentry.transport.ITransport import java.time.Instant @@ -93,7 +96,14 @@ class SentryAppenderTest { loggerContext.updateLoggers(config) - appender.start() + appender.start( + appender.getOptionsConfiguration { options -> + options.logs.loggerBatchProcessorFactory = + ILoggerBatchProcessorFactory { options, client -> + LoggerBatchProcessor(options, client, ImmediateExecutorService()) + } + } + ) loggerContext.start() return LogManager.getContext().getLogger(SentryAppenderTest::class.java.name) diff --git a/sentry-logback/src/test/kotlin/io/sentry/logback/SentryAppenderTest.kt b/sentry-logback/src/test/kotlin/io/sentry/logback/SentryAppenderTest.kt index e00d3aed49a..e93d6ef2db1 100644 --- a/sentry-logback/src/test/kotlin/io/sentry/logback/SentryAppenderTest.kt +++ b/sentry-logback/src/test/kotlin/io/sentry/logback/SentryAppenderTest.kt @@ -18,6 +18,9 @@ import io.sentry.SentryLogLevel import io.sentry.SentryOptions import io.sentry.checkEvent import io.sentry.checkLogs +import io.sentry.logger.ILoggerBatchProcessorFactory +import io.sentry.logger.LoggerBatchProcessor +import io.sentry.test.ImmediateExecutorService import io.sentry.test.applyTestOptions import io.sentry.test.initForTest import io.sentry.transport.ITransport @@ -69,6 +72,9 @@ class SentryAppenderTest { options.dsn = dsn options.isSendDefaultPii = sendDefaultPii options.logs.isEnabled = enableLogs + options.logs.loggerBatchProcessorFactory = ILoggerBatchProcessorFactory { options, client -> + LoggerBatchProcessor(options, client, ImmediateExecutorService()) + } applyTestOptions(options) contextTags?.forEach { options.addContextTag(it) } appender.setOptions(options) diff --git a/sentry/api/sentry.api b/sentry/api/sentry.api index 73eb9c61446..c8b194f32d3 100644 --- a/sentry/api/sentry.api +++ b/sentry/api/sentry.api @@ -5259,6 +5259,7 @@ public class io/sentry/logger/LoggerBatchProcessor : io/sentry/logger/ILoggerBat public static final field MAX_QUEUE_SIZE I protected final field options Lio/sentry/SentryOptions; public fun (Lio/sentry/SentryOptions;Lio/sentry/ISentryClient;)V + public fun (Lio/sentry/SentryOptions;Lio/sentry/ISentryClient;Lio/sentry/ISentryExecutorService;)V public fun add (Lio/sentry/SentryLogEvent;)V public fun close (Z)V public fun flush (J)V diff --git a/sentry/src/main/java/io/sentry/logger/LoggerBatchProcessor.java b/sentry/src/main/java/io/sentry/logger/LoggerBatchProcessor.java index 1f9b8fe7ce2..81ae5b73c1a 100644 --- a/sentry/src/main/java/io/sentry/logger/LoggerBatchProcessor.java +++ b/sentry/src/main/java/io/sentry/logger/LoggerBatchProcessor.java @@ -21,8 +21,10 @@ import java.util.concurrent.Future; import java.util.concurrent.RejectedExecutionException; import java.util.concurrent.TimeUnit; +import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import org.jetbrains.annotations.TestOnly; @Open public class LoggerBatchProcessor implements ILoggerBatchProcessor { @@ -44,10 +46,19 @@ public class LoggerBatchProcessor implements ILoggerBatchProcessor { public LoggerBatchProcessor( final @NotNull SentryOptions options, final @NotNull ISentryClient client) { + this(options, client, new SentryExecutorService(options)); + } + + @ApiStatus.Internal + @TestOnly + public LoggerBatchProcessor( + final @NotNull SentryOptions options, + final @NotNull ISentryClient client, + final @NotNull ISentryExecutorService executorService) { this.options = options; this.client = client; this.queue = new ConcurrentLinkedQueue<>(); - this.executorService = new SentryExecutorService(options); + this.executorService = executorService; } @Override