Timber 日志未在调试控制台或 Logcat 中打印

问题描述 投票:0回答:5
Log.i("Test", "Hello, Log")
Timber.i("Hello, Timber")

我可以在 Debug 控制台和 Logcat 中看到 Log.i 日志,但在任何地方都看不到 Timber 日志。

我/测试:你好,日志

我正在 stagingDebug 模式下构建。 (我有 4 个选项,生产调试和发布以及暂存调试和发布)

我缺少什么?

timber-android
5个回答
47
投票

确保您已在

Timber
类中初始化
Application
。以下代码可能会有所帮助:-

    public class MyApplication extends Application {
    
        @Override
        public void onCreate() {
            super.onCreate();

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

15
投票

如果您像这样初始化 Timber:

if (BuildConfig.DEBUG) {
    Timber.plant(Timber.DebugTree())
}

请确保您是从应用程序包导入 BuildConfig,而不是从第三方库导入 BuildConfig,如下所示:

import org.koin.android.BuildConfig
。我为此苦苦挣扎了几次。


14
投票

Kotlin 中的初始化 Timber

class ExampleApplication : Application(){

    override fun onCreate() {
        super.onCreate()
        // init timber
        if (BuildConfig.DEBUG) {
            Timber.plant(Timber.DebugTree())
        }
    }
}

并且不要忘记在 Manifest.xml 中写入 Application 的

android:name

<application
        android:name=".ExampleApplication"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.PostMakerMassive">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

5
投票

就我而言,我导入了错误的 BuildConfig。您可以检查一下是否也是您的问题。


1
投票

尝试在木材中添加

tag
。它应该根据清单中的信息(自动)设置,但并不总是发生(例如,对于具有自定义操作系统的自定义设备)

所以首先种植它:

// Java
Timber.plant(new Timber.DebugTree());

// Kotlin
Timber.plant(Timber.DebugTree())

和下一个设置标签:

// custom tag
Timber.tag("your custom tag");

// or
Timber.tag("trolololololo");

// or something (more serious) from strings:
Timber.tag(getString(R.string.app_name))
© www.soinside.com 2019 - 2024. All rights reserved.