Skip to content

Conversation

@markushi
Copy link
Member

@markushi markushi commented Nov 6, 2025

Summary

Adds log flushing functionality that triggers when the Android app transitions to the background.

Add flushLogs() method to ISentryClient interface and implement it in
SentryClient to allow log processors to flush any buffered logs when the
app transitions to the background. This is integrated into the lifecycle
watcher and can be enabled via an opt-in SentryOptions flag.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
@github-actions
Copy link
Contributor

github-actions bot commented Nov 6, 2025

Messages
📖 Do not forget to update Sentry-docs with your feature once the pull request gets approved.

Generated by 🚫 dangerJS against 2f205b3

@github-actions
Copy link
Contributor

github-actions bot commented Nov 6, 2025

Performance metrics 🚀

  Plain With Sentry Diff
Startup time 310.96 ms 361.33 ms 50.37 ms
Size 1.58 MiB 2.13 MiB 557.55 KiB

Baseline results on branch: main

Startup times

Revision Plain With Sentry Diff
d5a29b6 298.62 ms 391.78 ms 93.16 ms
806307f 357.85 ms 424.64 ms 66.79 ms
f634d01 375.06 ms 420.04 ms 44.98 ms
9fbb112 361.43 ms 427.57 ms 66.14 ms
3d205d0 352.15 ms 432.53 ms 80.38 ms
b77456b 393.26 ms 441.10 ms 47.84 ms
b3d8889 371.84 ms 447.49 ms 75.65 ms
a5ab36f 316.83 ms 394.54 ms 77.71 ms
ee747ae 357.79 ms 421.84 ms 64.05 ms
27d7cf8 309.43 ms 364.27 ms 54.85 ms

App size

Revision Plain With Sentry Diff
d5a29b6 1.58 MiB 2.12 MiB 549.37 KiB
806307f 1.58 MiB 2.10 MiB 533.42 KiB
f634d01 1.58 MiB 2.10 MiB 533.40 KiB
9fbb112 1.58 MiB 2.11 MiB 539.18 KiB
3d205d0 1.58 MiB 2.10 MiB 532.97 KiB
b77456b 1.58 MiB 2.12 MiB 548.11 KiB
b3d8889 1.58 MiB 2.10 MiB 535.07 KiB
a5ab36f 1.58 MiB 2.12 MiB 555.26 KiB
ee747ae 1.58 MiB 2.10 MiB 530.95 KiB
27d7cf8 1.58 MiB 2.12 MiB 549.42 KiB

Previous results on branch: feat/android-app-lifecycle-log-flushing

Startup times

Revision Plain With Sentry Diff
1d7703b 318.98 ms 405.18 ms 86.20 ms

App size

Revision Plain With Sentry Diff
1d7703b 1.58 MiB 2.12 MiB 551.96 KiB

*/
void flush(long timeoutMillis);

void flushLogs(long timeoutMillis);
Copy link
Member Author

Choose a reason for hiding this comment

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

Not the biggest fan of adding a new API here, an alternative would be to move this directly into ILoggerApi

Copy link
Member

Choose a reason for hiding this comment

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

if we're not going for https://github.com/getsentry/sentry-java/pull/4873/files#r2581483713, then I think this makes sense to make it cleaner and bake it into ILoggerApi instead

@stefanosiano
Copy link
Contributor

Isn't log processor flushing every 5 seconds anyway?
Is there a new requirement for this?
Or am i misunderstanding something?

@markushi
Copy link
Member Author

markushi commented Nov 6, 2025

Isn't log processor flushing every 5 seconds anyway? Is there a new requirement for this? Or am i misunderstanding something?

The spec mentions this:

  1. When the application moves to the background, the BatchProcessor SHOULD send all data in memory and stop the timer. This is mostly relevant for mobile SDKs.

```

</details>
- Android: Flush logs when app enters background ([#4873](https://github.com/getsentry/sentry-java/pull/4873))
Copy link
Member

Choose a reason for hiding this comment

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

maybe this should go above the previous entry, because it doesn't look right when rendered:
image


addAppBreadcrumb("background");

if (enableLogFlushing) {
Copy link
Member

Choose a reason for hiding this comment

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

hmm, wondering if it's a good idea to bind everything to LifecycleWatcher.. Since we now can addAppStateListener from anywhere, wdyt of introducing an AndroidLoggerApi which delegates everything to LoggerApi, but in addition subscribes for onBackground/onForeground to trigger logs flush? And then we override the Java LoggerApi via Options as always

Copy link
Member

@romtsn romtsn left a comment

Choose a reason for hiding this comment

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

a couple of suggestion, but not gonna block

@Override
public void run() {
scopes
.getGlobalScope()
Copy link
Member

Choose a reason for hiding this comment

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

m in theory there could be a different SentryClient instance on each scope type but that's probably more of an edge case. Romans suggestion above should get rid of that problem tho.

As an alternative, on Scopes there's already an implementation:

  @ApiStatus.Internal
  @NotNull
  public ISentryClient getClient() {
    return getCombinedScopeView().getClient();
  }

We'd just have to expose that on IScopes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants