Skip to content

Commit bc15877

Browse files
authored
Send logcat through Sentry Logs (#4487)
* SentryLogcatAdapter now forwards output to Sentry Logs, if enabled
1 parent f43539f commit bc15877

File tree

4 files changed

+166
-92
lines changed

4 files changed

+166
-92
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
### Fixes
6+
7+
- Send logcat through Sentry Logs ([#4487](https://github.com/getsentry/sentry-java/pull/4487))
8+
- Enable the Logs feature in your `SentryOptions` or with the `io.sentry.logs.enabled` manifest option and the SDK will automatically send logcat logs to Sentry, if the Sentry Android Gradle plugin is applied.
9+
- To set the logcat level check the [Logcat integration documentation](https://docs.sentry.io/platforms/android/integrations/logcat/#configure).
10+
311
## 8.16.1-alpha.2
412

513
### Fixes

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
import android.util.Log;
44
import io.sentry.Breadcrumb;
5+
import io.sentry.ScopesAdapter;
56
import io.sentry.Sentry;
67
import io.sentry.SentryLevel;
8+
import io.sentry.SentryLogLevel;
79
import org.jetbrains.annotations.ApiStatus;
810
import org.jetbrains.annotations.NotNull;
911
import org.jetbrains.annotations.Nullable;
@@ -44,73 +46,104 @@ private static void addAsBreadcrumb(
4446
Sentry.addBreadcrumb(breadcrumb);
4547
}
4648

49+
private static void addAsLog(
50+
@NotNull final SentryLogLevel level,
51+
@Nullable final String msg,
52+
@Nullable final Throwable tr) {
53+
final @NotNull ScopesAdapter scopes = ScopesAdapter.getInstance();
54+
// Check if logs are enabled before doing expensive operations
55+
if (!scopes.getOptions().getLogs().isEnabled()) {
56+
return;
57+
}
58+
final @Nullable String trMessage = tr != null ? tr.getMessage() : null;
59+
if (tr == null || trMessage == null) {
60+
scopes.logger().log(level, msg);
61+
} else {
62+
scopes.logger().log(level, msg != null ? (msg + "\n" + trMessage) : trMessage);
63+
}
64+
}
65+
4766
public static int v(@Nullable String tag, @Nullable String msg) {
4867
addAsBreadcrumb(tag, SentryLevel.DEBUG, msg);
68+
addAsLog(SentryLogLevel.TRACE, msg, null);
4969
return Log.v(tag, msg);
5070
}
5171

5272
public static int v(@Nullable String tag, @Nullable String msg, @Nullable Throwable tr) {
5373
addAsBreadcrumb(tag, SentryLevel.DEBUG, msg, tr);
74+
addAsLog(SentryLogLevel.TRACE, msg, tr);
5475
return Log.v(tag, msg, tr);
5576
}
5677

5778
public static int d(@Nullable String tag, @Nullable String msg) {
5879
addAsBreadcrumb(tag, SentryLevel.DEBUG, msg);
80+
addAsLog(SentryLogLevel.DEBUG, msg, null);
5981
return Log.d(tag, msg);
6082
}
6183

6284
public static int d(@Nullable String tag, @Nullable String msg, @Nullable Throwable tr) {
6385
addAsBreadcrumb(tag, SentryLevel.DEBUG, msg, tr);
86+
addAsLog(SentryLogLevel.DEBUG, msg, tr);
6487
return Log.d(tag, msg, tr);
6588
}
6689

6790
public static int i(@Nullable String tag, @Nullable String msg) {
6891
addAsBreadcrumb(tag, SentryLevel.INFO, msg);
92+
addAsLog(SentryLogLevel.INFO, msg, null);
6993
return Log.i(tag, msg);
7094
}
7195

7296
public static int i(@Nullable String tag, @Nullable String msg, @Nullable Throwable tr) {
7397
addAsBreadcrumb(tag, SentryLevel.INFO, msg, tr);
98+
addAsLog(SentryLogLevel.INFO, msg, tr);
7499
return Log.i(tag, msg, tr);
75100
}
76101

77102
public static int w(@Nullable String tag, @Nullable String msg) {
78103
addAsBreadcrumb(tag, SentryLevel.WARNING, msg);
104+
addAsLog(SentryLogLevel.WARN, msg, null);
79105
return Log.w(tag, msg);
80106
}
81107

82108
public static int w(@Nullable String tag, @Nullable String msg, @Nullable Throwable tr) {
83109
addAsBreadcrumb(tag, SentryLevel.WARNING, msg, tr);
110+
addAsLog(SentryLogLevel.WARN, msg, tr);
84111
return Log.w(tag, msg, tr);
85112
}
86113

87114
public static int w(@Nullable String tag, @Nullable Throwable tr) {
88115
addAsBreadcrumb(tag, SentryLevel.WARNING, tr);
116+
addAsLog(SentryLogLevel.WARN, null, tr);
89117
return Log.w(tag, tr);
90118
}
91119

92120
public static int e(@Nullable String tag, @Nullable String msg) {
93121
addAsBreadcrumb(tag, SentryLevel.ERROR, msg);
122+
addAsLog(SentryLogLevel.ERROR, msg, null);
94123
return Log.e(tag, msg);
95124
}
96125

97126
public static int e(@Nullable String tag, @Nullable String msg, @Nullable Throwable tr) {
98127
addAsBreadcrumb(tag, SentryLevel.ERROR, msg, tr);
128+
addAsLog(SentryLogLevel.ERROR, msg, tr);
99129
return Log.e(tag, msg, tr);
100130
}
101131

102132
public static int wtf(@Nullable String tag, @Nullable String msg) {
103133
addAsBreadcrumb(tag, SentryLevel.ERROR, msg);
134+
addAsLog(SentryLogLevel.FATAL, msg, null);
104135
return Log.wtf(tag, msg);
105136
}
106137

107138
public static int wtf(@Nullable String tag, @Nullable Throwable tr) {
108139
addAsBreadcrumb(tag, SentryLevel.ERROR, tr);
140+
addAsLog(SentryLogLevel.FATAL, null, tr);
109141
return Log.wtf(tag, tr);
110142
}
111143

112144
public static int wtf(@Nullable String tag, @Nullable String msg, @Nullable Throwable tr) {
113145
addAsBreadcrumb(tag, SentryLevel.ERROR, msg, tr);
146+
addAsLog(SentryLogLevel.FATAL, msg, tr);
114147
return Log.wtf(tag, msg, tr);
115148
}
116149
}

sentry-android-core/src/test/java/io/sentry/android/core/InternalSentrySdkTest.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,9 @@ class InternalSentrySdkTest {
269269

270270
// then modifications should not be reflected
271271
Sentry.configureScope { scope -> assertEquals(3, scope.breadcrumbs.size) }
272+
273+
// Ensure we don't interfere with other tests
274+
Sentry.configureScope(ScopeType.GLOBAL) { scope -> scope.clear() }
272275
}
273276

274277
@Test

0 commit comments

Comments
 (0)