自定义ReleaseTree不显示使用Timber的日志

时间:2019-02-06 10:31:16

标签: android timber release-builds timber-android

我正在使用Timber为我的应用程序使用自定义ReleaseTree来过滤日志。但是在构建发行版apk应用程序时,日志未显示在logcat中。在发行版中使用Timber.DebugTree()时,构建日志会正确显示。

这是我的ReleaseTree类:

class ReleaseTree : Timber.Tree() {

    override fun log(priority: Int, tag: String?, message: String, throwable: Throwable?) {
        // Don't log VERBOSE, DEBUG and INFO
        if (priority == Log.VERBOSE) {
            return
        }


        if (priority == Log.ERROR){
            val t = throwable ?: Exception(message)

            // Crashlytics
            Crashlytics.setInt(CRASHLYTICS_KEY_PRIORITY, priority)
            Crashlytics.setString(CRASHLYTICS_KEY_TAG, tag)
            Crashlytics.setString(CRASHLYTICS_KEY_MESSAGE, message)
            Crashlytics.logException(t)
        }

    }

    companion object {
        private val CRASHLYTICS_KEY_PRIORITY = "Priority"
        private val CRASHLYTICS_KEY_TAG = "Tag"
        private val CRASHLYTICS_KEY_MESSAGE = "Message"
    }
}

我如何从Application类初始化ReleaseTree:

Fabric.with(this, new Crashlytics());
Timber.plant(new ReleaseTree());

1 个答案:

答案 0 :(得分:1)

方法日志中缺少一行。

您需要从自定义树登录logcat

override fun log(priority: Int, tag: String?, message: String, throwable: Throwable?) {
        // Don't log VERBOSE, DEBUG and INFO
        if (priority == Log.VERBOSE) {
            return
        } else {
          Log.println(priority, tag, message);
        }


        if (priority == Log.ERROR){
            val t = throwable ?: Exception(message)

            // Crashlytics
            Crashlytics.setInt(CRASHLYTICS_KEY_PRIORITY, priority)
            Crashlytics.setString(CRASHLYTICS_KEY_TAG, tag)
            Crashlytics.setString(CRASHLYTICS_KEY_MESSAGE, message)
            Crashlytics.logException(t)
        }

    }

相关问题