|
2 | 2 |
|
3 | 3 | import android.util.Log;
|
4 | 4 | import io.sentry.Breadcrumb;
|
| 5 | +import io.sentry.ScopesAdapter; |
5 | 6 | import io.sentry.Sentry;
|
6 | 7 | import io.sentry.SentryLevel;
|
| 8 | +import io.sentry.SentryLogLevel; |
7 | 9 | import org.jetbrains.annotations.ApiStatus;
|
8 | 10 | import org.jetbrains.annotations.NotNull;
|
9 | 11 | import org.jetbrains.annotations.Nullable;
|
@@ -44,73 +46,104 @@ private static void addAsBreadcrumb(
|
44 | 46 | Sentry.addBreadcrumb(breadcrumb);
|
45 | 47 | }
|
46 | 48 |
|
| 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 | + |
47 | 66 | public static int v(@Nullable String tag, @Nullable String msg) {
|
48 | 67 | addAsBreadcrumb(tag, SentryLevel.DEBUG, msg);
|
| 68 | + addAsLog(SentryLogLevel.TRACE, msg, null); |
49 | 69 | return Log.v(tag, msg);
|
50 | 70 | }
|
51 | 71 |
|
52 | 72 | public static int v(@Nullable String tag, @Nullable String msg, @Nullable Throwable tr) {
|
53 | 73 | addAsBreadcrumb(tag, SentryLevel.DEBUG, msg, tr);
|
| 74 | + addAsLog(SentryLogLevel.TRACE, msg, tr); |
54 | 75 | return Log.v(tag, msg, tr);
|
55 | 76 | }
|
56 | 77 |
|
57 | 78 | public static int d(@Nullable String tag, @Nullable String msg) {
|
58 | 79 | addAsBreadcrumb(tag, SentryLevel.DEBUG, msg);
|
| 80 | + addAsLog(SentryLogLevel.DEBUG, msg, null); |
59 | 81 | return Log.d(tag, msg);
|
60 | 82 | }
|
61 | 83 |
|
62 | 84 | public static int d(@Nullable String tag, @Nullable String msg, @Nullable Throwable tr) {
|
63 | 85 | addAsBreadcrumb(tag, SentryLevel.DEBUG, msg, tr);
|
| 86 | + addAsLog(SentryLogLevel.DEBUG, msg, tr); |
64 | 87 | return Log.d(tag, msg, tr);
|
65 | 88 | }
|
66 | 89 |
|
67 | 90 | public static int i(@Nullable String tag, @Nullable String msg) {
|
68 | 91 | addAsBreadcrumb(tag, SentryLevel.INFO, msg);
|
| 92 | + addAsLog(SentryLogLevel.INFO, msg, null); |
69 | 93 | return Log.i(tag, msg);
|
70 | 94 | }
|
71 | 95 |
|
72 | 96 | public static int i(@Nullable String tag, @Nullable String msg, @Nullable Throwable tr) {
|
73 | 97 | addAsBreadcrumb(tag, SentryLevel.INFO, msg, tr);
|
| 98 | + addAsLog(SentryLogLevel.INFO, msg, tr); |
74 | 99 | return Log.i(tag, msg, tr);
|
75 | 100 | }
|
76 | 101 |
|
77 | 102 | public static int w(@Nullable String tag, @Nullable String msg) {
|
78 | 103 | addAsBreadcrumb(tag, SentryLevel.WARNING, msg);
|
| 104 | + addAsLog(SentryLogLevel.WARN, msg, null); |
79 | 105 | return Log.w(tag, msg);
|
80 | 106 | }
|
81 | 107 |
|
82 | 108 | public static int w(@Nullable String tag, @Nullable String msg, @Nullable Throwable tr) {
|
83 | 109 | addAsBreadcrumb(tag, SentryLevel.WARNING, msg, tr);
|
| 110 | + addAsLog(SentryLogLevel.WARN, msg, tr); |
84 | 111 | return Log.w(tag, msg, tr);
|
85 | 112 | }
|
86 | 113 |
|
87 | 114 | public static int w(@Nullable String tag, @Nullable Throwable tr) {
|
88 | 115 | addAsBreadcrumb(tag, SentryLevel.WARNING, tr);
|
| 116 | + addAsLog(SentryLogLevel.WARN, null, tr); |
89 | 117 | return Log.w(tag, tr);
|
90 | 118 | }
|
91 | 119 |
|
92 | 120 | public static int e(@Nullable String tag, @Nullable String msg) {
|
93 | 121 | addAsBreadcrumb(tag, SentryLevel.ERROR, msg);
|
| 122 | + addAsLog(SentryLogLevel.ERROR, msg, null); |
94 | 123 | return Log.e(tag, msg);
|
95 | 124 | }
|
96 | 125 |
|
97 | 126 | public static int e(@Nullable String tag, @Nullable String msg, @Nullable Throwable tr) {
|
98 | 127 | addAsBreadcrumb(tag, SentryLevel.ERROR, msg, tr);
|
| 128 | + addAsLog(SentryLogLevel.ERROR, msg, tr); |
99 | 129 | return Log.e(tag, msg, tr);
|
100 | 130 | }
|
101 | 131 |
|
102 | 132 | public static int wtf(@Nullable String tag, @Nullable String msg) {
|
103 | 133 | addAsBreadcrumb(tag, SentryLevel.ERROR, msg);
|
| 134 | + addAsLog(SentryLogLevel.FATAL, msg, null); |
104 | 135 | return Log.wtf(tag, msg);
|
105 | 136 | }
|
106 | 137 |
|
107 | 138 | public static int wtf(@Nullable String tag, @Nullable Throwable tr) {
|
108 | 139 | addAsBreadcrumb(tag, SentryLevel.ERROR, tr);
|
| 140 | + addAsLog(SentryLogLevel.FATAL, null, tr); |
109 | 141 | return Log.wtf(tag, tr);
|
110 | 142 | }
|
111 | 143 |
|
112 | 144 | public static int wtf(@Nullable String tag, @Nullable String msg, @Nullable Throwable tr) {
|
113 | 145 | addAsBreadcrumb(tag, SentryLevel.ERROR, msg, tr);
|
| 146 | + addAsLog(SentryLogLevel.FATAL, msg, tr); |
114 | 147 | return Log.wtf(tag, msg, tr);
|
115 | 148 | }
|
116 | 149 | }
|
0 commit comments