Skip to content

Commit d035656

Browse files
authored
Merge ead3f38 into e16d062
2 parents e16d062 + ead3f38 commit d035656

File tree

3 files changed

+155
-79
lines changed

3 files changed

+155
-79
lines changed

CHANGELOG.md

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

3+
## Unreleased
4+
5+
### Features
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+
310
## 8.13.3
411

512
### Fixes

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

Lines changed: 35 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,104 @@ 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+
if (tr == null) {
57+
scopes.logger().log(level, msg);
58+
} else {
59+
StringWriter sw = new StringWriter(256);
60+
PrintWriter pw = new PrintWriter(sw, false);
61+
tr.printStackTrace(pw);
62+
pw.flush();
63+
scopes.logger().log(level, msg != null ? (msg + "\n" + sw.toString()) : sw.toString());
64+
pw.close();
65+
}
66+
}
67+
4768
public static int v(@Nullable String tag, @Nullable String msg) {
4869
addAsBreadcrumb(tag, SentryLevel.DEBUG, msg);
70+
addAsLog(SentryLogLevel.TRACE, msg, null);
4971
return Log.v(tag, msg);
5072
}
5173

5274
public static int v(@Nullable String tag, @Nullable String msg, @Nullable Throwable tr) {
5375
addAsBreadcrumb(tag, SentryLevel.DEBUG, msg, tr);
76+
addAsLog(SentryLogLevel.TRACE, msg, tr);
5477
return Log.v(tag, msg, tr);
5578
}
5679

5780
public static int d(@Nullable String tag, @Nullable String msg) {
5881
addAsBreadcrumb(tag, SentryLevel.DEBUG, msg);
82+
addAsLog(SentryLogLevel.DEBUG, msg, null);
5983
return Log.d(tag, msg);
6084
}
6185

6286
public static int d(@Nullable String tag, @Nullable String msg, @Nullable Throwable tr) {
6387
addAsBreadcrumb(tag, SentryLevel.DEBUG, msg, tr);
88+
addAsLog(SentryLogLevel.DEBUG, msg, tr);
6489
return Log.d(tag, msg, tr);
6590
}
6691

6792
public static int i(@Nullable String tag, @Nullable String msg) {
6893
addAsBreadcrumb(tag, SentryLevel.INFO, msg);
94+
addAsLog(SentryLogLevel.INFO, msg, null);
6995
return Log.i(tag, msg);
7096
}
7197

7298
public static int i(@Nullable String tag, @Nullable String msg, @Nullable Throwable tr) {
7399
addAsBreadcrumb(tag, SentryLevel.INFO, msg, tr);
100+
addAsLog(SentryLogLevel.INFO, msg, tr);
74101
return Log.i(tag, msg, tr);
75102
}
76103

77104
public static int w(@Nullable String tag, @Nullable String msg) {
78105
addAsBreadcrumb(tag, SentryLevel.WARNING, msg);
106+
addAsLog(SentryLogLevel.WARN, msg, null);
79107
return Log.w(tag, msg);
80108
}
81109

82110
public static int w(@Nullable String tag, @Nullable String msg, @Nullable Throwable tr) {
83111
addAsBreadcrumb(tag, SentryLevel.WARNING, msg, tr);
112+
addAsLog(SentryLogLevel.WARN, msg, tr);
84113
return Log.w(tag, msg, tr);
85114
}
86115

87116
public static int w(@Nullable String tag, @Nullable Throwable tr) {
88117
addAsBreadcrumb(tag, SentryLevel.WARNING, tr);
118+
addAsLog(SentryLogLevel.WARN, null, tr);
89119
return Log.w(tag, tr);
90120
}
91121

92122
public static int e(@Nullable String tag, @Nullable String msg) {
93123
addAsBreadcrumb(tag, SentryLevel.ERROR, msg);
124+
addAsLog(SentryLogLevel.ERROR, msg, null);
94125
return Log.e(tag, msg);
95126
}
96127

97128
public static int e(@Nullable String tag, @Nullable String msg, @Nullable Throwable tr) {
98129
addAsBreadcrumb(tag, SentryLevel.ERROR, msg, tr);
130+
addAsLog(SentryLogLevel.ERROR, msg, tr);
99131
return Log.e(tag, msg, tr);
100132
}
101133

102134
public static int wtf(@Nullable String tag, @Nullable String msg) {
103135
addAsBreadcrumb(tag, SentryLevel.ERROR, msg);
136+
addAsLog(SentryLogLevel.FATAL, msg, null);
104137
return Log.wtf(tag, msg);
105138
}
106139

107140
public static int wtf(@Nullable String tag, @Nullable Throwable tr) {
108141
addAsBreadcrumb(tag, SentryLevel.ERROR, tr);
142+
addAsLog(SentryLogLevel.FATAL, null, tr);
109143
return Log.wtf(tag, tr);
110144
}
111145

112146
public static int wtf(@Nullable String tag, @Nullable String msg, @Nullable Throwable tr) {
113147
addAsBreadcrumb(tag, SentryLevel.ERROR, msg, tr);
148+
addAsLog(SentryLogLevel.FATAL, msg, tr);
114149
return Log.wtf(tag, msg, tr);
115150
}
116151
}

0 commit comments

Comments
 (0)