|
| 1 | +package io.sentry.android.core; |
| 2 | + |
| 3 | +import android.util.Log; |
| 4 | +import io.sentry.Breadcrumb; |
| 5 | +import io.sentry.Sentry; |
| 6 | +import io.sentry.SentryLevel; |
| 7 | +import org.jetbrains.annotations.ApiStatus; |
| 8 | +import org.jetbrains.annotations.NotNull; |
| 9 | +import org.jetbrains.annotations.Nullable; |
| 10 | + |
| 11 | +/** |
| 12 | + * This class replaces {@link android.util.Log} with its own implementations which creates a {@link |
| 13 | + * io.sentry.Breadcrumb} for each log. It only replaces log functions that meet a minimum level set |
| 14 | + * by the user on the Sentry Android Gradle Plugin. |
| 15 | + */ |
| 16 | +@ApiStatus.Internal |
| 17 | +public final class SentryLogcatAdapter { |
| 18 | + |
| 19 | + private static void addAsBreadcrumb( |
| 20 | + @Nullable String tag, @NotNull SentryLevel level, @Nullable String msg) { |
| 21 | + addAsBreadcrumb(tag, level, msg, null); |
| 22 | + } |
| 23 | + |
| 24 | + private static void addAsBreadcrumb( |
| 25 | + @Nullable String tag, @NotNull SentryLevel level, @Nullable Throwable tr) { |
| 26 | + addAsBreadcrumb(tag, level, null, tr); |
| 27 | + } |
| 28 | + |
| 29 | + private static void addAsBreadcrumb( |
| 30 | + @Nullable final String tag, |
| 31 | + @NotNull final SentryLevel level, |
| 32 | + @Nullable final String msg, |
| 33 | + @Nullable final Throwable tr) { |
| 34 | + Breadcrumb breadcrumb = new Breadcrumb(); |
| 35 | + breadcrumb.setCategory("Logcat"); |
| 36 | + breadcrumb.setMessage(msg); |
| 37 | + breadcrumb.setLevel(level); |
| 38 | + if (tag != null) { |
| 39 | + breadcrumb.setData("tag", tag); |
| 40 | + } |
| 41 | + if (tr != null && tr.getMessage() != null) { |
| 42 | + breadcrumb.setData("throwable", tr.getMessage()); |
| 43 | + } |
| 44 | + Sentry.addBreadcrumb(breadcrumb); |
| 45 | + } |
| 46 | + |
| 47 | + public static int v(@Nullable String tag, @Nullable String msg) { |
| 48 | + addAsBreadcrumb(tag, SentryLevel.DEBUG, msg); |
| 49 | + return Log.v(tag, msg); |
| 50 | + } |
| 51 | + |
| 52 | + public static int v(@Nullable String tag, @Nullable String msg, @Nullable Throwable tr) { |
| 53 | + addAsBreadcrumb(tag, SentryLevel.DEBUG, msg, tr); |
| 54 | + return Log.v(tag, msg, tr); |
| 55 | + } |
| 56 | + |
| 57 | + public static int d(@Nullable String tag, @Nullable String msg) { |
| 58 | + addAsBreadcrumb(tag, SentryLevel.DEBUG, msg); |
| 59 | + return Log.d(tag, msg); |
| 60 | + } |
| 61 | + |
| 62 | + public static int d(@Nullable String tag, @Nullable String msg, @Nullable Throwable tr) { |
| 63 | + addAsBreadcrumb(tag, SentryLevel.DEBUG, msg, tr); |
| 64 | + return Log.d(tag, msg, tr); |
| 65 | + } |
| 66 | + |
| 67 | + public static int i(@Nullable String tag, @Nullable String msg) { |
| 68 | + addAsBreadcrumb(tag, SentryLevel.INFO, msg); |
| 69 | + return Log.i(tag, msg); |
| 70 | + } |
| 71 | + |
| 72 | + public static int i(@Nullable String tag, @Nullable String msg, @Nullable Throwable tr) { |
| 73 | + addAsBreadcrumb(tag, SentryLevel.INFO, msg, tr); |
| 74 | + return Log.i(tag, msg, tr); |
| 75 | + } |
| 76 | + |
| 77 | + public static int w(@Nullable String tag, @Nullable String msg) { |
| 78 | + addAsBreadcrumb(tag, SentryLevel.WARNING, msg); |
| 79 | + return Log.w(tag, msg); |
| 80 | + } |
| 81 | + |
| 82 | + public static int w(@Nullable String tag, @Nullable String msg, @Nullable Throwable tr) { |
| 83 | + addAsBreadcrumb(tag, SentryLevel.WARNING, msg, tr); |
| 84 | + return Log.w(tag, msg, tr); |
| 85 | + } |
| 86 | + |
| 87 | + public static int w(@Nullable String tag, @Nullable Throwable tr) { |
| 88 | + addAsBreadcrumb(tag, SentryLevel.WARNING, tr); |
| 89 | + return Log.w(tag, tr); |
| 90 | + } |
| 91 | + |
| 92 | + public static int e(@Nullable String tag, @Nullable String msg) { |
| 93 | + addAsBreadcrumb(tag, SentryLevel.ERROR, msg); |
| 94 | + return Log.e(tag, msg); |
| 95 | + } |
| 96 | + |
| 97 | + public static int e(@Nullable String tag, @Nullable String msg, @Nullable Throwable tr) { |
| 98 | + addAsBreadcrumb(tag, SentryLevel.ERROR, msg, tr); |
| 99 | + return Log.e(tag, msg, tr); |
| 100 | + } |
| 101 | + |
| 102 | + public static int wtf(@Nullable String tag, @Nullable String msg) { |
| 103 | + addAsBreadcrumb(tag, SentryLevel.ERROR, msg); |
| 104 | + return Log.wtf(tag, msg); |
| 105 | + } |
| 106 | + |
| 107 | + public static int wtf(@Nullable String tag, @Nullable Throwable tr) { |
| 108 | + addAsBreadcrumb(tag, SentryLevel.ERROR, tr); |
| 109 | + return Log.wtf(tag, tr); |
| 110 | + } |
| 111 | + |
| 112 | + public static int wtf(@Nullable String tag, @Nullable String msg, @Nullable Throwable tr) { |
| 113 | + addAsBreadcrumb(tag, SentryLevel.ERROR, msg, tr); |
| 114 | + return Log.wtf(tag, msg, tr); |
| 115 | + } |
| 116 | +} |
0 commit comments