26

Timber 是一个很棒的 Android 登录库。但在 Kotlin 类中,不输出任何内容。我怎样才能解决这个问题?

MainActivity.kt 代码:

Timber.e("Timber Log 1")
Log.e("MainActivity", "Log 1")

Gradle:我尝试过常规的 Java Timber:

implementation 'com.jakewharton.timber:timber:4.7.1'

这个 Kotlin 特定的包装器:

implementation 'com.github.ajalt:timberkt:1.5.1'

结果相同。两者都没有输出。仅从Log.e()

4

5 回答 5

31

Timber 的第一步是种植文档中提到的树

行为是通过 Tree 实例添加的。您可以通过调用 Timber.plant 来安装实例。应尽早安装树木。应用程序的 onCreate 是最合乎逻辑的选择。

并使用debugTree

DebugTree implementation will automatically figure out from which class it's being called and use that class name as its tag。_ 由于标签不同

如果您不这样做,那么您将没有日志条目并尽快执行此操作,例如在oncreate应用程序类中或更好地执行此操作

Timber.plant(Timber.DebugTree());
于 2018-07-08T21:05:15.747 回答
15

我遇到了同样的问题,使用 Kotlin 和 Android studio 3.6 请按照以下步骤操作:

  1. 在 build.gradle(Module: App) 中添加以下内容

    implementation 'com.jakewharton.timber:timber:4.7.1'
    
  2. Timber在应用程序类中初始化:

    class MyApp : Application() {
    
        override fun onCreate() {
            super.onCreate()
    
            if(BuildConfig.DEBUG){
                Timber.plant(Timber.DebugTree())
            }
        }
    }
    
  3. 将 Application class( MyApp) 添加到 Manifest (AndroidManifest.xml)

    <application
        android:name=".MyApp"
    

现在您可以使用木材:Timber.i("Timber logs")

如果您愿意,也可以使用自定义标签:Timber.tag("Yo").I("used custom tag for logs")

于 2020-07-21T07:58:41.790 回答
1

聚会可能迟到了,但我的问题是我的手机设置为“仅充电”而不是“文件传输”。显然我被允许构建和运行,但日志被阻止

于 2021-03-16T09:42:31.883 回答
0

对我来说,当我评论调试检查时它开始出现

//        if (BuildConfig.DEBUG) {
            Timber.plant(new Timber.DebugTree());
//        }

我不知道为什么这会起作用,因为选择构建变体仅用于调试。

于 2022-01-05T07:53:22.167 回答
0

就我而言,这是错误的 BuildConfig 导入

import org.koin.android.BuildConfig

但我的应用程序有

import com.company.example.BuildConfig
于 2022-01-19T10:31:01.103 回答