android - 防止 FirebaseCrash.logcat() 记录到 logcat

标签 android logcat firebase-crash-reporting timber-android

我在我的应用程序中使用 Timber 和 DebugTree。我想将所有消息记录到 Firebase 崩溃报告中。

Timber.plant(object :  Timber.DebugTree() {
    override fun log(priority: Int, tag: String?, message: String?, t: Throwable?) {
        FirebaseCrash.logcat(priority, tag, message)
    }
})

但是现在所有消息都在 logcat 中重复,看起来像这样

11-07 17:08:07.823 V/GoogleApiClientPlugin: Connected to GooglePlayClient
11-07 17:08:07.823 V/GoogleApiClientPlugin: Connected to GooglePlayClient
11-07 17:08:07.824 V/NearbyForegroundService: connected to Google Api Client
11-07 17:08:07.824 V/NearbyForegroundService: connected to Google Api Client

我不想用 if (BuildConfig.DEBUG) { ... } 保护调用,因为日志也应该附加到调试构建中发生的崩溃。

Fabric 具有相同的行为,但我可以将记录器设置为静默 .logger(new SilentLogger()) 以防止重复的日志消息。是否有用于 Firebase 的 API?

最佳答案

只需使用 FirebaseCrash.log(String mess)记录消息的方法

这里是我使用的代码:

public class FirebaseBone extends Dog.Bone {

@Override
String getTag() {
    return "Shadow";
}

@Override
protected boolean isLoggable(String tag, int priority) {
    return super.isLoggable(tag, priority);
}

@Override
protected void log(int priority, String tag, String message, Throwable t) {
    String sb = getPriority(priority) +
            (TextUtils.isEmpty(tag) ? "" : ("/" + tag)) +
            ": " +
            message;

    FirebaseCrash.log(sb);
    if (t != null) {
        FirebaseCrash.report(t);
    }
}

private String getPriority(int priority) {
    switch (priority) {
        case Log.ASSERT:
            return "A";
        case Log.DEBUG:
            return "D";
        case Log.ERROR:
            return "E";
        case Log.INFO:
            return "I";
        case Log.VERBOSE:
            return "V";
        case Log.WARN:
            return "W";
        default:
            return "?";
    }
}
}

关于android - 防止 FirebaseCrash.logcat() 记录到 logcat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40469485/

相关文章:

android - Linux操作系统下的2D安卓游戏开发工具

android - 从下载的应用程序获取日志信息

android - 如何将映射文件上传到 Firebase?

android - 将按钮添加到android中的gridview

java - 从其他类(扩展广播接收器)调用 MainActivity 方法

android - 合子 : isWhitelistProcess - Process is Whitelisted

firebase - BigQuery Crashlytics-免费崩溃的用户/ session

android - 拦截 crashlytics 隐私对话框按钮单击

java - 安卓Java : BoofCV Integration Problems

Android audiorecord 实例失败