Skip to content

Commit 2e00745

Browse files
authored
Merge f8ec564 into b292090
2 parents b292090 + f8ec564 commit 2e00745

File tree

4 files changed

+154
-84
lines changed

4 files changed

+154
-84
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
### Fixes
66

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.
79
- No longer send out empty log envelopes ([#4497](https://github.com/getsentry/sentry-java/pull/4497))
810

911
### Dependencies

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
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;
9+
import java.io.PrintWriter;
10+
import java.io.StringWriter;
711
import org.jetbrains.annotations.ApiStatus;
812
import org.jetbrains.annotations.NotNull;
913
import org.jetbrains.annotations.Nullable;
@@ -44,73 +48,108 @@ private static void addAsBreadcrumb(
4448
Sentry.addBreadcrumb(breadcrumb);
4549
}
4650

51+
private static void addAsLog(
52+
@NotNull final SentryLogLevel level,
53+
@Nullable final String msg,
54+
@Nullable final Throwable tr) {
55+
final @NotNull ScopesAdapter scopes = ScopesAdapter.getInstance();
56+
// Check if logs are enabled before doing expensive operations
57+
if (!scopes.getOptions().getLogs().isEnabled()) {
58+
return;
59+
}
60+
if (tr == null) {
61+
scopes.logger().log(level, msg);
62+
} else {
63+
StringWriter sw = new StringWriter(256);
64+
PrintWriter pw = new PrintWriter(sw, false);
65+
tr.printStackTrace(pw);
66+
pw.flush();
67+
scopes.logger().log(level, msg != null ? (msg + "\n" + sw.toString()) : sw.toString());
68+
pw.close();
69+
}
70+
}
71+
4772
public static int v(@Nullable String tag, @Nullable String msg) {
4873
addAsBreadcrumb(tag, SentryLevel.DEBUG, msg);
74+
addAsLog(SentryLogLevel.TRACE, msg, null);
4975
return Log.v(tag, msg);
5076
}
5177

5278
public static int v(@Nullable String tag, @Nullable String msg, @Nullable Throwable tr) {
5379
addAsBreadcrumb(tag, SentryLevel.DEBUG, msg, tr);
80+
addAsLog(SentryLogLevel.TRACE, msg, tr);
5481
return Log.v(tag, msg, tr);
5582
}
5683

5784
public static int d(@Nullable String tag, @Nullable String msg) {
5885
addAsBreadcrumb(tag, SentryLevel.DEBUG, msg);
86+
addAsLog(SentryLogLevel.DEBUG, msg, null);
5987
return Log.d(tag, msg);
6088
}
6189

6290
public static int d(@Nullable String tag, @Nullable String msg, @Nullable Throwable tr) {
6391
addAsBreadcrumb(tag, SentryLevel.DEBUG, msg, tr);
92+
addAsLog(SentryLogLevel.DEBUG, msg, tr);
6493
return Log.d(tag, msg, tr);
6594
}
6695

6796
public static int i(@Nullable String tag, @Nullable String msg) {
6897
addAsBreadcrumb(tag, SentryLevel.INFO, msg);
98+
addAsLog(SentryLogLevel.INFO, msg, null);
6999
return Log.i(tag, msg);
70100
}
71101

72102
public static int i(@Nullable String tag, @Nullable String msg, @Nullable Throwable tr) {
73103
addAsBreadcrumb(tag, SentryLevel.INFO, msg, tr);
104+
addAsLog(SentryLogLevel.INFO, msg, tr);
74105
return Log.i(tag, msg, tr);
75106
}
76107

77108
public static int w(@Nullable String tag, @Nullable String msg) {
78109
addAsBreadcrumb(tag, SentryLevel.WARNING, msg);
110+
addAsLog(SentryLogLevel.WARN, msg, null);
79111
return Log.w(tag, msg);
80112
}
81113

82114
public static int w(@Nullable String tag, @Nullable String msg, @Nullable Throwable tr) {
83115
addAsBreadcrumb(tag, SentryLevel.WARNING, msg, tr);
116+
addAsLog(SentryLogLevel.WARN, msg, tr);
84117
return Log.w(tag, msg, tr);
85118
}
86119

87120
public static int w(@Nullable String tag, @Nullable Throwable tr) {
88121
addAsBreadcrumb(tag, SentryLevel.WARNING, tr);
122+
addAsLog(SentryLogLevel.WARN, null, tr);
89123
return Log.w(tag, tr);
90124
}
91125

92126
public static int e(@Nullable String tag, @Nullable String msg) {
93127
addAsBreadcrumb(tag, SentryLevel.ERROR, msg);
128+
addAsLog(SentryLogLevel.ERROR, msg, null);
94129
return Log.e(tag, msg);
95130
}
96131

97132
public static int e(@Nullable String tag, @Nullable String msg, @Nullable Throwable tr) {
98133
addAsBreadcrumb(tag, SentryLevel.ERROR, msg, tr);
134+
addAsLog(SentryLogLevel.ERROR, msg, tr);
99135
return Log.e(tag, msg, tr);
100136
}
101137

102138
public static int wtf(@Nullable String tag, @Nullable String msg) {
103139
addAsBreadcrumb(tag, SentryLevel.ERROR, msg);
140+
addAsLog(SentryLogLevel.FATAL, msg, null);
104141
return Log.wtf(tag, msg);
105142
}
106143

107144
public static int wtf(@Nullable String tag, @Nullable Throwable tr) {
108145
addAsBreadcrumb(tag, SentryLevel.ERROR, tr);
146+
addAsLog(SentryLogLevel.FATAL, null, tr);
109147
return Log.wtf(tag, tr);
110148
}
111149

112150
public static int wtf(@Nullable String tag, @Nullable String msg, @Nullable Throwable tr) {
113151
addAsBreadcrumb(tag, SentryLevel.ERROR, msg, tr);
152+
addAsLog(SentryLogLevel.FATAL, msg, tr);
114153
return Log.wtf(tag, msg, tr);
115154
}
116155
}

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
@@ -241,6 +241,9 @@ class InternalSentrySdkTest {
241241
Sentry.configureScope { scope ->
242242
assertEquals(3, scope.breadcrumbs.size)
243243
}
244+
245+
// Ensure we don't interfere with other tests
246+
Sentry.configureScope(ScopeType.GLOBAL) { scope -> scope.clear() }
244247
}
245248

246249
@Test

0 commit comments

Comments
 (0)