Skip to content

Commit cbfbab8

Browse files
authored
Merge pull request #5945 from getsentry/feat/logs-opt-in-logcat
feat(android): [Logs and Metrics Enable Flags 5] Add Logcat Logs opt-in
2 parents 1c7e8e6 + 228fb0e commit cbfbab8

8 files changed

Lines changed: 110 additions & 5 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
### Features
66

7+
- Add an explicit Logs opt-in to the Android Logcat integration ([#5945](https://github.com/getsentry/sentry-java/pull/5945))
78
- Add an explicit Logs opt-in to the Android Timber integration ([#5943](https://github.com/getsentry/sentry-java/pull/5943))
89
- Add an explicit Logs opt-in to the JUL handler ([#5942](https://github.com/getsentry/sentry-java/pull/5942))
910
- Add an explicit Logs opt-in to the Log4j2 appender ([#5941](https://github.com/getsentry/sentry-java/pull/5941))

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,7 @@ public final class io/sentry/android/core/SentryAndroidOptions : io/sentry/Sentr
453453
public fun isEnableAutoActivityLifecycleTracing ()Z
454454
public fun isEnableAutoTraceIdGeneration ()Z
455455
public fun isEnableFramesTracking ()Z
456+
public fun isEnableLogcatLogs ()Z
456457
public fun isEnableNdk ()Z
457458
public fun isEnableNdkAppHangTracking ()Z
458459
public fun isEnableNetworkEventBreadcrumbs ()Z
@@ -489,6 +490,7 @@ public final class io/sentry/android/core/SentryAndroidOptions : io/sentry/Sentr
489490
public fun setEnableAutoActivityLifecycleTracing (Z)V
490491
public fun setEnableAutoTraceIdGeneration (Z)V
491492
public fun setEnableFramesTracking (Z)V
493+
public fun setEnableLogcatLogs (Z)V
492494
public fun setEnableNdk (Z)V
493495
public fun setEnableNdkAppHangTracking (Z)V
494496
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
@@ -196,6 +196,8 @@ final class ManifestMetadataReader {
196196

197197
static final String ENABLE_TIMBER_LOGS = "io.sentry.timber.logs.enabled";
198198

199+
static final String ENABLE_LOGCAT_LOGS = "io.sentry.logcat.logs.enabled";
200+
199201
static final String ENABLE_METRICS = "io.sentry.metrics.enabled";
200202

201203
static final String ENABLE_AUTO_TRACE_ID_GENERATION =
@@ -759,6 +761,9 @@ static void applyMetadata(
759761
options.setEnableTimberLogs(
760762
readBool(metadata, logger, ENABLE_TIMBER_LOGS, options.isEnableTimberLogs()));
761763

764+
options.setEnableLogcatLogs(
765+
readBool(metadata, logger, ENABLE_LOGCAT_LOGS, options.isEnableLogcatLogs()));
766+
762767
options
763768
.getMetrics()
764769
.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
@@ -78,6 +78,9 @@ public final class SentryAndroidOptions extends SentryOptions {
7878
/** Enable or disable automatic Sentry Logs capture from Timber. Default is disabled. */
7979
private boolean enableTimberLogs = false;
8080

81+
/** Enable or disable automatic Sentry Logs capture from Logcat. Default is disabled. */
82+
private boolean enableLogcatLogs = false;
83+
8184
/**
8285
* Enables the Auto instrumentation for Activity lifecycle tracing.
8386
*
@@ -516,6 +519,14 @@ public void setEnableTimberLogs(boolean enableTimberLogs) {
516519
this.enableTimberLogs = enableTimberLogs;
517520
}
518521

522+
public boolean isEnableLogcatLogs() {
523+
return enableLogcatLogs;
524+
}
525+
526+
public void setEnableLogcatLogs(boolean enableLogcatLogs) {
527+
this.enableLogcatLogs = enableLogcatLogs;
528+
}
529+
519530
/**
520531
* Enable or disable all the automatic breadcrumbs
521532
*

‎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: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2219,6 +2219,36 @@ class ManifestMetadataReaderTest {
22192219
assertTrue(fixture.options.isEnableTimberLogs)
22202220
}
22212221

2222+
@Test
2223+
fun `applyMetadata keeps Logcat logs disabled if not found`() {
2224+
val context = fixture.getContext()
2225+
2226+
ManifestMetadataReader.applyMetadata(context, fixture.options, fixture.buildInfoProvider)
2227+
2228+
assertThat(fixture.options.isEnableLogcatLogs).isFalse()
2229+
}
2230+
2231+
@Test
2232+
fun `applyMetadata reads Logcat logs enabled to options`() {
2233+
val bundle = bundleOf(ManifestMetadataReader.ENABLE_LOGCAT_LOGS to true)
2234+
val context = fixture.getContext(metaData = bundle)
2235+
2236+
ManifestMetadataReader.applyMetadata(context, fixture.options, fixture.buildInfoProvider)
2237+
2238+
assertThat(fixture.options.isEnableLogcatLogs).isTrue()
2239+
}
2240+
2241+
@Test
2242+
fun `applyMetadata reads Logcat logs disabled to options`() {
2243+
fixture.options.isEnableLogcatLogs = true
2244+
val bundle = bundleOf(ManifestMetadataReader.ENABLE_LOGCAT_LOGS to false)
2245+
val context = fixture.getContext(metaData = bundle)
2246+
2247+
ManifestMetadataReader.applyMetadata(context, fixture.options, fixture.buildInfoProvider)
2248+
2249+
assertThat(fixture.options.isEnableLogcatLogs).isFalse()
2250+
}
2251+
22222252
@Test
22232253
fun `applyMetadata reads metrics enabled and keep default value if not found`() {
22242254
// 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)