Skip to content

Commit 6a65f6f

Browse files
adinauerclaude
andcommitted
feat(android): Add Logcat Logs opt-in
Require an explicit Android option before forwarding Logcat calls as Sentry Logs. Support Java and manifest configuration while keeping breadcrumbs and Android Log calls unchanged. Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 4f677fb commit 6a65f6f

7 files changed

Lines changed: 110 additions & 5 deletions

File tree

‎sentry-android-core/api/sentry-android-core.api‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,7 @@ public final class io/sentry/android/core/SentryAndroidOptions : io/sentry/Sentr
430430
public fun isEnableAutoActivityLifecycleTracing ()Z
431431
public fun isEnableAutoTraceIdGeneration ()Z
432432
public fun isEnableFramesTracking ()Z
433+
public fun isEnableLogcatLogs ()Z
433434
public fun isEnableNdk ()Z
434435
public fun isEnableNdkAppHangTracking ()Z
435436
public fun isEnableNetworkEventBreadcrumbs ()Z
@@ -464,6 +465,7 @@ public final class io/sentry/android/core/SentryAndroidOptions : io/sentry/Sentr
464465
public fun setEnableAutoActivityLifecycleTracing (Z)V
465466
public fun setEnableAutoTraceIdGeneration (Z)V
466467
public fun setEnableFramesTracking (Z)V
468+
public fun setEnableLogcatLogs (Z)V
467469
public fun setEnableNdk (Z)V
468470
public fun setEnableNdkAppHangTracking (Z)V
469471
public fun setEnableNetworkEventBreadcrumbs (Z)V

‎sentry-android-core/src/main/java/io/sentry/android/core/ManifestMetadataReader.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,8 @@ final class ManifestMetadataReader {
165165

166166
static final String ENABLE_TIMBER_LOGS = "io.sentry.timber.logs.enabled";
167167

168+
static final String ENABLE_LOGCAT_LOGS = "io.sentry.logcat.logs.enabled";
169+
168170
static final String ENABLE_METRICS = "io.sentry.metrics.enabled";
169171

170172
static final String ENABLE_AUTO_TRACE_ID_GENERATION =
@@ -711,6 +713,9 @@ static void applyMetadata(
711713
options.setEnableTimberLogs(
712714
readBool(metadata, logger, ENABLE_TIMBER_LOGS, options.isEnableTimberLogs()));
713715

716+
options.setEnableLogcatLogs(
717+
readBool(metadata, logger, ENABLE_LOGCAT_LOGS, options.isEnableLogcatLogs()));
718+
714719
options
715720
.getMetrics()
716721
.setEnabled(

‎sentry-android-core/src/main/java/io/sentry/android/core/SentryAndroidOptions.java‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ public final class SentryAndroidOptions extends SentryOptions {
7474
/** Enable or disable automatic Sentry Logs capture from Timber. Default is disabled. */
7575
private boolean enableTimberLogs = false;
7676

77+
/** Enable or disable automatic Sentry Logs capture from Logcat. Default is disabled. */
78+
private boolean enableLogcatLogs = false;
79+
7780
/**
7881
* Enables the Auto instrumentation for Activity lifecycle tracing.
7982
*
@@ -468,6 +471,14 @@ public void setEnableTimberLogs(boolean enableTimberLogs) {
468471
this.enableTimberLogs = enableTimberLogs;
469472
}
470473

474+
public boolean isEnableLogcatLogs() {
475+
return enableLogcatLogs;
476+
}
477+
478+
public void setEnableLogcatLogs(boolean enableLogcatLogs) {
479+
this.enableLogcatLogs = enableLogcatLogs;
480+
}
481+
471482
/**
472483
* Enable or disable all the automatic breadcrumbs
473484
*

‎sentry-android-core/src/main/java/io/sentry/android/core/SentryLogcatAdapter.java‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import io.sentry.Sentry;
77
import io.sentry.SentryLevel;
88
import io.sentry.SentryLogLevel;
9+
import io.sentry.SentryOptions;
910
import io.sentry.logger.SentryLogParameters;
1011
import org.jetbrains.annotations.ApiStatus;
1112
import org.jetbrains.annotations.NotNull;
@@ -52,8 +53,10 @@ private static void addAsLog(
5253
@Nullable final String msg,
5354
@Nullable final Throwable tr) {
5455
final @NotNull ScopesAdapter scopes = ScopesAdapter.getInstance();
55-
// Check if logs are enabled before doing expensive operations
56-
if (!scopes.getOptions().getLogs().isEnabled()) {
56+
final @NotNull SentryOptions options = scopes.getOptions();
57+
if (!(options instanceof SentryAndroidOptions)
58+
|| !((SentryAndroidOptions) options).isEnableLogcatLogs()
59+
|| !options.getLogs().isEnabled()) {
5760
return;
5861
}
5962
final @Nullable String trMessage = tr != null ? tr.getMessage() : null;

‎sentry-android-core/src/test/java/io/sentry/android/core/ManifestMetadataReaderTest.kt‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import android.content.Context
44
import android.os.Bundle
55
import androidx.core.os.bundleOf
66
import androidx.test.ext.junit.runners.AndroidJUnit4
7+
import com.google.common.truth.Truth.assertThat
78
import io.sentry.FilterString
89
import io.sentry.ILogger
910
import io.sentry.ProfileLifecycle
@@ -1986,6 +1987,36 @@ class ManifestMetadataReaderTest {
19861987
assertTrue(fixture.options.isEnableTimberLogs)
19871988
}
19881989

1990+
@Test
1991+
fun `applyMetadata keeps Logcat logs disabled if not found`() {
1992+
val context = fixture.getContext()
1993+
1994+
ManifestMetadataReader.applyMetadata(context, fixture.options, fixture.buildInfoProvider)
1995+
1996+
assertThat(fixture.options.isEnableLogcatLogs).isFalse()
1997+
}
1998+
1999+
@Test
2000+
fun `applyMetadata reads Logcat logs enabled to options`() {
2001+
val bundle = bundleOf(ManifestMetadataReader.ENABLE_LOGCAT_LOGS to true)
2002+
val context = fixture.getContext(metaData = bundle)
2003+
2004+
ManifestMetadataReader.applyMetadata(context, fixture.options, fixture.buildInfoProvider)
2005+
2006+
assertThat(fixture.options.isEnableLogcatLogs).isTrue()
2007+
}
2008+
2009+
@Test
2010+
fun `applyMetadata reads Logcat logs disabled to options`() {
2011+
fixture.options.isEnableLogcatLogs = true
2012+
val bundle = bundleOf(ManifestMetadataReader.ENABLE_LOGCAT_LOGS to false)
2013+
val context = fixture.getContext(metaData = bundle)
2014+
2015+
ManifestMetadataReader.applyMetadata(context, fixture.options, fixture.buildInfoProvider)
2016+
2017+
assertThat(fixture.options.isEnableLogcatLogs).isFalse()
2018+
}
2019+
19892020
@Test
19902021
fun `applyMetadata reads metrics enabled and keep default value if not found`() {
19912022
// Arrange

‎sentry-android-core/src/test/java/io/sentry/android/core/SentryAndroidOptionsTest.kt‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.sentry.android.core
22

3+
import com.google.common.truth.Truth.assertThat
34
import io.sentry.ITransactionProfiler
45
import io.sentry.NoOpTransactionProfiler
56
import io.sentry.protocol.DebugImage
@@ -108,6 +109,19 @@ class SentryAndroidOptionsTest {
108109
assertTrue(sentryOptions.isEnableTimberLogs)
109110
}
110111

112+
@Test
113+
fun `Logcat logs are disabled by default`() {
114+
assertThat(SentryAndroidOptions().isEnableLogcatLogs).isFalse()
115+
}
116+
117+
@Test
118+
fun `Logcat logs can be enabled`() {
119+
val sentryOptions = SentryAndroidOptions()
120+
sentryOptions.isEnableLogcatLogs = true
121+
122+
assertThat(sentryOptions.isEnableLogcatLogs).isTrue()
123+
}
124+
111125
@Test
112126
fun `attach screenshots disabled by default for Android`() {
113127
val sentryOptions = SentryAndroidOptions()

‎sentry-android-core/src/test/java/io/sentry/android/core/SentryLogcatAdapterTest.kt‎

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package io.sentry.android.core
22

33
import android.os.Bundle
44
import androidx.test.ext.junit.runners.AndroidJUnit4
5+
import com.google.common.truth.Truth.assertThat
56
import io.sentry.Breadcrumb
67
import io.sentry.Sentry
78
import io.sentry.SentryLevel
@@ -15,6 +16,7 @@ import kotlin.test.Test
1516
import kotlin.test.assertEquals
1617
import kotlin.test.assertTrue
1718
import org.junit.runner.RunWith
19+
import org.robolectric.shadows.ShadowLog
1820

1921
@RunWith(AndroidJUnit4::class)
2022
class SentryLogcatAdapterTest {
@@ -26,16 +28,22 @@ class SentryLogcatAdapterTest {
2628
val breadcrumbs = mutableListOf<Breadcrumb>()
2729
val logs = mutableListOf<SentryLogEvent>()
2830

29-
fun initSut(options: Sentry.OptionsConfiguration<SentryAndroidOptions>? = null) {
30-
val metadata =
31-
Bundle().apply { putString(ManifestMetadataReader.DSN, "https://key@sentry.io/123") }
31+
fun initSut(
32+
enableLogcatLogs: Boolean? = true,
33+
metadata: Bundle = Bundle(),
34+
options: Sentry.OptionsConfiguration<SentryAndroidOptions>? = null,
35+
) {
36+
metadata.putString(ManifestMetadataReader.DSN, "https://key@sentry.io/123")
3237
val mockContext = ContextUtilsTestHelper.mockMetaData(metaData = metadata)
3338
initForTest(mockContext) {
3439
it.beforeBreadcrumb = SentryOptions.BeforeBreadcrumbCallback { breadcrumb, _ ->
3540
breadcrumbs.add(breadcrumb)
3641
breadcrumb
3742
}
3843
it.logs.isEnabled = true
44+
if (enableLogcatLogs != null) {
45+
it.isEnableLogcatLogs = enableLogcatLogs
46+
}
3947
it.logs.beforeSend =
4048
SentryOptions.Logs.BeforeSendLogCallback { logEvent ->
4149
logs.add(logEvent)
@@ -55,6 +63,37 @@ class SentryLogcatAdapterTest {
5563
Sentry.close()
5664
fixture.breadcrumbs.clear()
5765
fixture.logs.clear()
66+
ShadowLog.clear()
67+
}
68+
69+
@Test
70+
fun `Logcat logs are disabled by default while breadcrumbs and Android Log remain enabled`() {
71+
fixture.initSut(enableLogcatLogs = null)
72+
73+
SentryLogcatAdapter.d(tag, commonMsg)
74+
75+
assertThat(fixture.logs).isEmpty()
76+
assertThat(fixture.breadcrumbs).hasSize(1)
77+
assertThat(ShadowLog.getLogs().any { it.tag == tag && it.msg == commonMsg }).isTrue()
78+
}
79+
80+
@Test
81+
fun `Logcat logs can be enabled through Android options`() {
82+
fixture.initSut(enableLogcatLogs = true)
83+
84+
SentryLogcatAdapter.d(tag, commonMsg)
85+
86+
assertThat(fixture.logs).hasSize(1)
87+
}
88+
89+
@Test
90+
fun `Logcat logs can be enabled through manifest metadata`() {
91+
val metadata = Bundle().apply { putBoolean(ManifestMetadataReader.ENABLE_LOGCAT_LOGS, true) }
92+
fixture.initSut(enableLogcatLogs = null, metadata = metadata)
93+
94+
SentryLogcatAdapter.d(tag, commonMsg)
95+
96+
assertThat(fixture.logs).hasSize(1)
5897
}
5998

6099
@Test

0 commit comments

Comments
 (0)