|
| 1 | +package io.sentry.jul |
| 2 | + |
| 3 | +import com.google.common.truth.Truth.assertThat |
| 4 | +import io.sentry.Sentry |
| 5 | +import io.sentry.SentryLevel |
| 6 | +import io.sentry.test.MockSentryServer |
| 7 | +import io.sentry.test.event |
| 8 | +import io.sentry.test.hasFlag |
| 9 | +import io.sentry.test.logs |
| 10 | +import java.util.logging.Level |
| 11 | +import java.util.logging.LogManager |
| 12 | +import java.util.logging.Logger |
| 13 | +import kotlin.test.AfterTest |
| 14 | +import kotlin.test.Test |
| 15 | + |
| 16 | +class SentryHandlerIntegrationTest { |
| 17 | + private val server = MockSentryServer() |
| 18 | + |
| 19 | + @AfterTest |
| 20 | + fun teardown() { |
| 21 | + // Back to the configuration the other tests read, which also closes this test's handler. |
| 22 | + LogManager.getLogManager().readConfiguration() |
| 23 | + Sentry.close() |
| 24 | + server.close() |
| 25 | + System.clearProperty("sentry.dsn") |
| 26 | + } |
| 27 | + |
| 28 | + @Test |
| 29 | + fun `handler configured from logging properties sends events, breadcrumbs and logs`() { |
| 30 | + // The handler reads the DSN as external configuration, as an app's sentry.properties would |
| 31 | + // set it. The module's test sentry.properties enables logs. |
| 32 | + System.setProperty("sentry.dsn", server.dsn) |
| 33 | + javaClass.getResourceAsStream("/sentry-handler-integration-test.properties")!!.use { |
| 34 | + LogManager.getLogManager().readConfiguration(it) |
| 35 | + } |
| 36 | + // Outside io.sentry, whose loggers the handler drops so it doesn't capture the SDK's own logs. |
| 37 | + val logger = Logger.getLogger("com.example.App") |
| 38 | + |
| 39 | + logger.config("Hello Sentry!") |
| 40 | + Sentry.setAttribute("user.type", "admin") |
| 41 | + Sentry.setAttribute("feature.version", 2) |
| 42 | + Sentry.setAttribute("debug.enabled", true) |
| 43 | + Sentry.addFeatureFlag("my-feature-flag", true) |
| 44 | + logger.warning("important warning") |
| 45 | + logger.log(Level.INFO, "User has made a purchase of product: %d", 445) |
| 46 | + logger.log(Level.SEVERE, "Something went wrong", RuntimeException("Invalid productId=445")) |
| 47 | + Sentry.flush(5000) |
| 48 | + |
| 49 | + val warning = server.awaitEnvelope { it.event()?.level == SentryLevel.WARNING }.event()!! |
| 50 | + assertThat(warning.message!!.message).isEqualTo("important warning") |
| 51 | + assertThat(warning.hasFlag("my-feature-flag", true)).isTrue() |
| 52 | + |
| 53 | + val error = server.awaitEnvelope { it.event()?.level == SentryLevel.ERROR }.event()!! |
| 54 | + assertThat(error.message!!.message).isEqualTo("Something went wrong") |
| 55 | + assertThat(error.exceptions!!.map { it.type to it.value }) |
| 56 | + .contains("RuntimeException" to "Invalid productId=445") |
| 57 | + assertThat(error.breadcrumbs!!.map { it.message to it.level }) |
| 58 | + .containsAtLeast( |
| 59 | + "Hello Sentry!" to SentryLevel.DEBUG, |
| 60 | + "User has made a purchase of product: 445" to SentryLevel.INFO, |
| 61 | + ) |
| 62 | + |
| 63 | + val logs = server.awaitEnvelope { it.logs() != null }.logs()!! |
| 64 | + assertThat(logs.items.map { it.body }).contains("User has made a purchase of product: 445") |
| 65 | + val failureLog = logs.items.single { it.body == "Something went wrong" } |
| 66 | + assertThat(failureLog.attributes!!.mapValues { it.value.value }) |
| 67 | + .containsAtLeast("user.type", "admin", "feature.version", 2, "debug.enabled", true) |
| 68 | + } |
| 69 | +} |
0 commit comments