Skip to content

Commit a9eeb78

Browse files
committed
When a crash occurs, logs are flushed
1 parent 85d7417 commit a9eeb78

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

sentry/src/main/java/io/sentry/SentryClient.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,12 @@ private boolean shouldApplyScopeData(final @NotNull CheckIn event, final @NotNul
245245
finalizeTransaction(scope, hint);
246246
}
247247

248+
// if event is backfillable or cached we don't need to flush the logs, because it's an event
249+
// from the past. Otherwise we need to flush the logs to ensure they are sent on crash
250+
if (event != null && !isBackfillable && !isCached && event.isCrashed()) {
251+
loggerBatchProcessor.flush(options.getFlushTimeoutMillis());
252+
}
253+
248254
return sentryId;
249255
}
250256

sentry/src/test/java/io/sentry/SentryClientTest.kt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3023,6 +3023,25 @@ class SentryClientTest {
30233023
assertTrue(terminated == true)
30243024
}
30253025

3026+
@Test
3027+
fun `flush logs for crash events`() {
3028+
val sut = fixture.getSut()
3029+
val batchProcessor = mock<ILoggerBatchProcessor>()
3030+
sut.injectForField("loggerBatchProcessor", batchProcessor)
3031+
sut.captureLog(
3032+
SentryLogEvent(SentryId(), SentryNanotimeDate(), "message", SentryLogLevel.WARN),
3033+
fixture.scopes.scope,
3034+
)
3035+
3036+
sut.captureEvent(
3037+
SentryEvent().apply {
3038+
exceptions =
3039+
listOf(SentryException().apply { mechanism = Mechanism().apply { isHandled = false } })
3040+
}
3041+
)
3042+
verify(batchProcessor).flush(any())
3043+
}
3044+
30263045
@Test
30273046
fun `cleans up replay folder for Backfillable replay events`() {
30283047
val dir = File(tmpDir.newFolder().absolutePath)

0 commit comments

Comments
 (0)