Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Features

- Add an explicit Logs opt-in to the JUL handler ([#5942](https://github.com/getsentry/sentry-java/pull/5942))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should probably consolidate these once the stack is merged

- Add an explicit Logs opt-in to the Log4j2 appender ([#5941](https://github.com/getsentry/sentry-java/pull/5941))
- Add an explicit Logs opt-in to the Logback appender ([#5940](https://github.com/getsentry/sentry-java/pull/5940))
- Make `ISpan.startChild` overloads with `SpanOptions` public ([#5927](https://github.com/getsentry/sentry-java/pull/5927))
Expand Down
2 changes: 2 additions & 0 deletions sentry-jul/api/sentry-jul.api
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ public class io/sentry/jul/SentryHandler : java/util/logging/Handler {
public fun getMinimumBreadcrumbLevel ()Ljava/util/logging/Level;
public fun getMinimumEventLevel ()Ljava/util/logging/Level;
public fun getMinimumLevel ()Ljava/util/logging/Level;
public fun isEnableLogs ()Z
public fun isPrintfStyle ()Z
public fun publish (Ljava/util/logging/LogRecord;)V
public fun setEnableLogs (Z)V
public fun setMinimumBreadcrumbLevel (Ljava/util/logging/Level;)V
public fun setMinimumEventLevel (Ljava/util/logging/Level;)V
public fun setMinimumLevel (Ljava/util/logging/Level;)V
Expand Down
14 changes: 13 additions & 1 deletion sentry-jul/src/main/java/io/sentry/jul/SentryHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public class SentryHandler extends Handler {
*/
private boolean printfStyle;

private boolean enableLogs;

private @NotNull Level minimumBreadcrumbLevel = Level.INFO;
private @NotNull Level minimumEventLevel = Level.SEVERE;
private @NotNull Level minimumLevel = Level.INFO;
Expand Down Expand Up @@ -112,7 +114,8 @@ public void publish(final @NotNull LogRecord record) {
return;
}
try {
if (ScopesAdapter.getInstance().getOptions().getLogs().isEnabled()
if (enableLogs
&& ScopesAdapter.getInstance().getOptions().getLogs().isEnabled()
&& record.getLevel().intValue() >= minimumLevel.intValue()) {
captureLog(record);
}
Expand Down Expand Up @@ -191,6 +194,7 @@ private void retrieveProperties() {
final LogManager manager = LogManager.getLogManager();
final String className = SentryHandler.class.getName();
setPrintfStyle(Boolean.parseBoolean(manager.getProperty(className + ".printfStyle")));
setEnableLogs(Boolean.parseBoolean(manager.getProperty(className + ".enableLogs")));
setLevel(parseLevelOrDefault(manager.getProperty(className + ".level")));
final String minimumBreadCrumbLevel =
manager.getProperty(className + ".minimumBreadcrumbLevel");
Expand Down Expand Up @@ -390,6 +394,14 @@ public void setPrintfStyle(final boolean printfStyle) {
this.printfStyle = printfStyle;
}

public void setEnableLogs(final boolean enableLogs) {
this.enableLogs = enableLogs;
}

public boolean isEnableLogs() {
return enableLogs;
}

public void setMinimumBreadcrumbLevel(final @Nullable Level minimumBreadcrumbLevel) {
if (minimumBreadcrumbLevel != null) {
this.minimumBreadcrumbLevel = minimumBreadcrumbLevel;
Expand Down
72 changes: 71 additions & 1 deletion sentry-jul/src/test/kotlin/io/sentry/jul/SentryHandlerTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import kotlin.test.assertNull
import kotlin.test.assertTrue
import org.mockito.kotlin.anyOrNull
import org.mockito.kotlin.mock
import org.mockito.kotlin.never
import org.mockito.kotlin.verify
import org.slf4j.MDC

Expand All @@ -40,6 +41,8 @@ class SentryHandlerTest {
val transport: ITransport = mock(),
contextTags: List<String>? = null,
printfStyle: Boolean? = null,
enableLogs: Boolean? = true,
enableGlobalLogs: Boolean = true,
) {
var logger: Logger
var handler: SentryHandler
Expand All @@ -48,6 +51,7 @@ class SentryHandlerTest {
val options = SentryOptions()
options.dsn = "http://key@localhost/proj"
options.setTransportFactory { _, _ -> transport }
options.logs.isEnabled = enableGlobalLogs
options.logs.loggerBatchProcessorFactory = ILoggerBatchProcessorFactory { options, client ->
LoggerBatchProcessor(options, client, ImmediateExecutorService())
}
Expand All @@ -58,6 +62,9 @@ class SentryHandlerTest {
handler.setMinimumBreadcrumbLevel(minimumBreadcrumbLevel)
handler.setMinimumEventLevel(minimumEventLevel)
handler.setMinimumLevel(minimumLevel)
if (enableLogs != null) {
handler.setEnableLogs(enableLogs)
}
if (printfStyle == true) {
handler.setPrintfStyle(printfStyle)
}
Expand Down Expand Up @@ -318,11 +325,22 @@ class SentryHandlerTest {

@Test
fun `fetches configuration from logging dot properties`() {
fixture = Fixture(configureWithLogManager = true)
fixture = Fixture(configureWithLogManager = true, enableLogs = null)
assertEquals(Level.CONFIG, fixture.handler.minimumBreadcrumbLevel)
assertEquals(Level.WARNING, fixture.handler.minimumEventLevel)
assertEquals(Level.ALL, fixture.handler.level)
assertTrue(fixture.handler.isPrintfStyle)
assertTrue(fixture.handler.isEnableLogs)

fixture.logger.info("this should be captured as a log")
Sentry.flush(10)

verify(fixture.transport)
.send(
checkLogs { logs ->
assertEquals("this should be captured as a log", logs.items.first().body)
}
)
}

@Test
Expand Down Expand Up @@ -418,6 +436,58 @@ class SentryHandlerTest {
)
}

@Test
fun `does not capture logs by default`() {
fixture = Fixture(enableLogs = null, enableGlobalLogs = true)

assertFalse(fixture.handler.isEnableLogs)
fixture.logger.info("this should not be captured as a log")
Sentry.flush(10)

verify(fixture.transport, never()).send(checkLogs {})
}

@Test
fun `captures logs when enabled through Java`() {
fixture = Fixture(enableLogs = true, enableGlobalLogs = true)

assertTrue(fixture.handler.isEnableLogs)
fixture.logger.info("this should be captured as a log")
Sentry.flush(10)

verify(fixture.transport)
.send(
checkLogs { logs ->
assertEquals("this should be captured as a log", logs.items.first().body)
}
)
}

@Test
fun `captures events and breadcrumbs when logs are disabled`() {
fixture =
Fixture(
minimumBreadcrumbLevel = Level.INFO,
minimumEventLevel = Level.SEVERE,
enableLogs = false,
enableGlobalLogs = true,
)

fixture.logger.info("this should be a breadcrumb")
fixture.logger.severe("this should be an event")
Sentry.flush(10)

verify(fixture.transport)
.send(
checkEvent { event ->
assertEquals("this should be an event", event.message?.message)
assertEquals("this should be a breadcrumb", event.breadcrumbs?.single()?.message)
},
anyOrNull(),
)
verify(fixture.transport, never()).send(checkLogs {})
}

@Test
fun `converts finest log level to Sentry log level`() {
fixture = Fixture(minimumLevel = Level.FINEST)
Expand Down
1 change: 1 addition & 0 deletions sentry-jul/src/test/resources/logging.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ io.sentry.jul.SentryHandler.minimumEventLevel=WARNING
io.sentry.jul.SentryHandler.minimumBreadcrumbLevel=CONFIG
io.sentry.jul.SentryHandler.minimumLevel=CONFIG
io.sentry.jul.SentryHandler.printfStyle=true
io.sentry.jul.SentryHandler.enableLogs=true

jul.SentryHandlerTest.handlers=java.util.logging.ConsoleHandler, io.sentry.jul.SentryHandler
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ io.sentry.jul.SentryHandler.minimumEventLevel=INFO
io.sentry.jul.SentryHandler.minimumBreadcrumbLevel=CONFIG
io.sentry.jul.SentryHandler.minimumLevel=INFO
io.sentry.jul.SentryHandler.printfStyle=true
io.sentry.jul.SentryHandler.enableLogs=true
io.sentry.jul.SentryHandler.level=FINEST
java.util.logging.ConsoleHandler.level = FINE
handlers=io.sentry.jul.SentryHandler
Expand Down
Loading