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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
- Replace `UUIDGenerator` implementation with Apache licensed code ([#4662](https://github.com/getsentry/sentry-java/pull/4662))
- Replace `Random` implementation with MIT licensed code ([#4664](https://github.com/getsentry/sentry-java/pull/4664))

### Fixes

- Flush logs on crash ([#4684](https://github.com/getsentry/sentry-java/pull/4684))

## 8.20.0

### Fixes
Expand Down
7 changes: 7 additions & 0 deletions sentry/src/main/java/io/sentry/SentryClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
public final class SentryClient implements ISentryClient {
static final String SENTRY_PROTOCOL_VERSION = "7";

private static final int LOG_FLUSH_ON_CRASH_TIMEOUT_MILLIS = 500;

private boolean enabled;

private final @NotNull SentryOptions options;
Expand Down Expand Up @@ -244,6 +246,11 @@ private boolean shouldApplyScopeData(final @NotNull CheckIn event, final @NotNul
if (scope != null) {
finalizeTransaction(scope, hint);
}
// if event is backfillable or cached, it's an event from the past.
// Otherwise, we want to flush logs, as we encountered a crash.
if (event != null && !isBackfillable && !isCached && event.isCrashed()) {
loggerBatchProcessor.flush(LOG_FLUSH_ON_CRASH_TIMEOUT_MILLIS);
}

return sentryId;
}
Expand Down
19 changes: 19 additions & 0 deletions sentry/src/test/java/io/sentry/SentryClientTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3023,6 +3023,25 @@ class SentryClientTest {
assertTrue(terminated == true)
}

@Test
fun `flush logs for crash events`() {
val sut = fixture.getSut()
val batchProcessor = mock<ILoggerBatchProcessor>()
sut.injectForField("loggerBatchProcessor", batchProcessor)
sut.captureLog(
SentryLogEvent(SentryId(), SentryNanotimeDate(), "message", SentryLogLevel.WARN),
fixture.scopes.scope,
)

sut.captureEvent(
SentryEvent().apply {
exceptions =
listOf(SentryException().apply { mechanism = Mechanism().apply { isHandled = false } })
}
)
verify(batchProcessor).flush(eq(500))
}

@Test
fun `cleans up replay folder for Backfillable replay events`() {
val dir = File(tmpDir.newFolder().absolutePath)
Expand Down
Loading