-
-
Notifications
You must be signed in to change notification settings - Fork 463
feat(android): Add log flushing on app backgrounding #4951
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
markushi
merged 4 commits into
main
from
markushi/feat/android-app-lifecycle-log-flushing-2
Dec 4, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
sentry-android-core/src/main/java/io/sentry/android/core/AndroidLoggerBatchProcessor.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| package io.sentry.android.core; | ||
|
|
||
| import io.sentry.ISentryClient; | ||
| import io.sentry.SentryLevel; | ||
| import io.sentry.SentryOptions; | ||
| import io.sentry.logger.LoggerBatchProcessor; | ||
| import org.jetbrains.annotations.ApiStatus; | ||
| import org.jetbrains.annotations.NotNull; | ||
|
|
||
| @ApiStatus.Internal | ||
| public final class AndroidLoggerBatchProcessor extends LoggerBatchProcessor | ||
| implements AppState.AppStateListener { | ||
|
|
||
| public AndroidLoggerBatchProcessor( | ||
| @NotNull SentryOptions options, @NotNull ISentryClient client) { | ||
| super(options, client); | ||
| AppState.getInstance().addAppStateListener(this); | ||
| } | ||
|
|
||
| @Override | ||
| public void onForeground() { | ||
| // no-op | ||
| } | ||
|
|
||
| @Override | ||
| public void onBackground() { | ||
| try { | ||
| options | ||
| .getExecutorService() | ||
| .submit( | ||
| new Runnable() { | ||
| @Override | ||
| public void run() { | ||
| flush(LoggerBatchProcessor.FLUSH_AFTER_MS); | ||
| } | ||
| }); | ||
| } catch (Throwable t) { | ||
| options.getLogger().log(SentryLevel.ERROR, t, "Failed to submit log flush in onBackground()"); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void close(boolean isRestarting) { | ||
| AppState.getInstance().removeAppStateListener(this); | ||
| super.close(isRestarting); | ||
| } | ||
| } | ||
15 changes: 15 additions & 0 deletions
15
...android-core/src/main/java/io/sentry/android/core/AndroidLoggerBatchProcessorFactory.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| package io.sentry.android.core; | ||
|
|
||
| import io.sentry.SentryClient; | ||
| import io.sentry.SentryOptions; | ||
| import io.sentry.logger.ILoggerBatchProcessor; | ||
| import io.sentry.logger.ILoggerBatchProcessorFactory; | ||
| import org.jetbrains.annotations.NotNull; | ||
|
|
||
| public final class AndroidLoggerBatchProcessorFactory implements ILoggerBatchProcessorFactory { | ||
| @Override | ||
| public @NotNull ILoggerBatchProcessor create( | ||
| @NotNull SentryOptions options, @NotNull SentryClient client) { | ||
| return new AndroidLoggerBatchProcessor(options, client); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
...droid-core/src/test/java/io/sentry/android/core/AndroidLoggerBatchProcessorFactoryTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| package io.sentry.android.core | ||
|
|
||
| import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
| import io.sentry.SentryClient | ||
| import kotlin.test.Test | ||
| import kotlin.test.assertIs | ||
| import org.junit.runner.RunWith | ||
| import org.mockito.kotlin.mock | ||
|
|
||
| @RunWith(AndroidJUnit4::class) | ||
| class AndroidLoggerBatchProcessorFactoryTest { | ||
|
|
||
| @Test | ||
| fun `create returns AndroidLoggerBatchProcessor instance`() { | ||
| val factory = AndroidLoggerBatchProcessorFactory() | ||
| val options = SentryAndroidOptions() | ||
| val client: SentryClient = mock() | ||
|
|
||
| val processor = factory.create(options, client) | ||
|
|
||
| assertIs<AndroidLoggerBatchProcessor>(processor) | ||
| } | ||
| } |
97 changes: 97 additions & 0 deletions
97
sentry-android-core/src/test/java/io/sentry/android/core/AndroidLoggerBatchProcessorTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| package io.sentry.android.core | ||
|
|
||
| import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
| import io.sentry.ISentryClient | ||
| import io.sentry.SentryLogEvent | ||
| import io.sentry.SentryLogLevel | ||
| import io.sentry.SentryOptions | ||
| import io.sentry.protocol.SentryId | ||
| import io.sentry.test.ImmediateExecutorService | ||
| import kotlin.test.AfterTest | ||
| import kotlin.test.BeforeTest | ||
| import kotlin.test.Test | ||
| import kotlin.test.assertNotNull | ||
| import kotlin.test.assertTrue | ||
| import org.junit.runner.RunWith | ||
| import org.mockito.kotlin.any | ||
| import org.mockito.kotlin.mock | ||
| import org.mockito.kotlin.verify | ||
| import org.mockito.kotlin.whenever | ||
|
|
||
| @RunWith(AndroidJUnit4::class) | ||
| class AndroidLoggerBatchProcessorTest { | ||
|
|
||
| private class Fixture { | ||
| val options = SentryAndroidOptions() | ||
| val client: ISentryClient = mock() | ||
|
|
||
| fun getSut( | ||
| useImmediateExecutor: Boolean = false, | ||
| config: ((SentryOptions) -> Unit)? = null, | ||
| ): AndroidLoggerBatchProcessor { | ||
| if (useImmediateExecutor) { | ||
| options.executorService = ImmediateExecutorService() | ||
| } | ||
| config?.invoke(options) | ||
| return AndroidLoggerBatchProcessor(options, client) | ||
| } | ||
| } | ||
|
|
||
| private val fixture = Fixture() | ||
|
|
||
| @BeforeTest | ||
| fun `set up`() { | ||
| AppState.getInstance().resetInstance() | ||
| } | ||
|
|
||
| @AfterTest | ||
| fun `tear down`() { | ||
| AppState.getInstance().resetInstance() | ||
| } | ||
|
|
||
| @Test | ||
| fun `constructor registers as AppState listener`() { | ||
| fixture.getSut() | ||
| assertNotNull(AppState.getInstance().lifecycleObserver) | ||
| } | ||
|
|
||
| @Test | ||
| fun `onBackground schedules flush`() { | ||
| val sut = fixture.getSut(useImmediateExecutor = true) | ||
| val logEvent = SentryLogEvent(SentryId(), 1.0, "test", SentryLogLevel.INFO) | ||
| sut.add(logEvent) | ||
|
|
||
| sut.onBackground() | ||
|
|
||
| verify(fixture.client).captureBatchedLogEvents(any()) | ||
| } | ||
|
|
||
| @Test | ||
| fun `onBackground handles executor exception gracefully`() { | ||
| val sut = | ||
| fixture.getSut { options -> | ||
| val rejectingExecutor = mock<io.sentry.ISentryExecutorService>() | ||
| whenever(rejectingExecutor.submit(any())).thenThrow(RuntimeException("Rejected")) | ||
| options.executorService = rejectingExecutor | ||
| } | ||
|
|
||
| // Should not throw | ||
| sut.onBackground() | ||
| } | ||
|
|
||
| @Test | ||
| fun `close removes AppState listener`() { | ||
| val sut = fixture.getSut() | ||
| sut.close(false) | ||
|
|
||
| assertTrue(AppState.getInstance().lifecycleObserver.listeners.isEmpty()) | ||
| } | ||
|
|
||
| @Test | ||
| fun `close with isRestarting true still removes listener`() { | ||
| val sut = fixture.getSut() | ||
| sut.close(true) | ||
|
|
||
| assertTrue(AppState.getInstance().lifecycleObserver.listeners.isEmpty()) | ||
markushi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
sentry/src/main/java/io/sentry/logger/DefaultLoggerBatchProcessorFactory.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package io.sentry.logger; | ||
|
|
||
| import io.sentry.SentryClient; | ||
| import io.sentry.SentryOptions; | ||
| import org.jetbrains.annotations.NotNull; | ||
|
|
||
| public final class DefaultLoggerBatchProcessorFactory implements ILoggerBatchProcessorFactory { | ||
| @Override | ||
| public @NotNull ILoggerBatchProcessor create( | ||
| @NotNull SentryOptions options, @NotNull SentryClient client) { | ||
| return new LoggerBatchProcessor(options, client); | ||
| } | ||
| } |
12 changes: 12 additions & 0 deletions
12
sentry/src/main/java/io/sentry/logger/ILoggerBatchProcessorFactory.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package io.sentry.logger; | ||
|
|
||
| import io.sentry.SentryClient; | ||
| import io.sentry.SentryOptions; | ||
| import org.jetbrains.annotations.NotNull; | ||
|
|
||
| public interface ILoggerBatchProcessorFactory { | ||
|
|
||
| @NotNull | ||
| ILoggerBatchProcessor create( | ||
| final @NotNull SentryOptions options, final @NotNull SentryClient client); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.