首页 > 解决方案 > Android kotlin 日志库

问题描述

我需要一个用于 android 的日志库,它将:

现在,我知道有很多关于 android 在 stackoverflow 上登录的问题,但我想找到适合我所有需求的库。如果您知道一个好的,我将非常感谢您的帮助。

谢谢!

标签: androidkotlinlogging

解决方案


首先,我推荐使用Timber来管理日志(这个库支持用于调试和生产目的的日志)。

然后,您需要编写一个函数将日志写入磁盘。

例如:

if (BuildConfig.DEBUG) {
    // Default debug tree 
    // A Tree for debug builds. Automatically infers the tag from the calling class. 
    Timber.plant(Timber.DebugTree())
} else {
    // Implement your custom Tree if need, eg. enable crashlytics,...
}

DebugTree您现在可以编写函数以通过覆盖类将日志写入磁盘:

Timber.plant(object: Timber.DebugTree() {

        // for example, if you need to write log when catched exceptions
        override fun e(t: Throwable?) {
            super.e(t)
        }

        // or here to check for all tags
        override fun createStackElementTag(element: StackTraceElement): String? {
            return super.createStackElementTag(element)
        }
})


推荐阅读