34

Timber is a great library for logging in Android. In Kotlin classes though, doesn't output anything. How can I fix this?

MainActivity.kt code:

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

Gradle: I've tried the regular Java Timber:

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

And this Kotlin specific wrapper:

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

Same result. No output with either. Only from the Log.e()

5 Answers 5

38

The first step of Timber is to plant the tree as mentioned in docs

Behavior is added through Tree instances. You can install an instance by calling Timber.plant. Installation of Trees should be done as early as possible. The onCreate of your application is the most logical choice.

And use the debugTree

The DebugTree implementation will automatically figure out from which class it's being called and use that class name as its tag. Since the tags vary

If you don't do this then you will have no logs entry and do this as soon as possible, like in oncreate or better inside application class so do this

Timber.plant(Timber.DebugTree());
4
  • Ah jeez, I got so wrapped up in the Kotlin I forgot to initialize the logger. Thanks a lot!
    – Ethan_AI
    Commented Jul 8, 2018 at 21:07
  • as you guys already know, it's Timber.plant(DebugTree()); in kotlin, missed the kotlin tag Commented Jul 15, 2018 at 21:00
  • @IliaSidorenko 'cause it's catchy! Commented Feb 26, 2020 at 13:22
  • what about using DI (Hilt)? logs can happen earlier - before Timber.plant(Timber.DebugTree()) is called when Hilt creates your dependencies
    – user924
    Commented Apr 6 at 13:17
28

I have faced same problem, using Kotlin and Android studio 3.6 Follow these steps:

  1. Add the following in build.gradle(Module: App)

    implementation 'com.jakewharton.timber:timber:4.7.1'
    
  2. Initialize Timber in Application Class:

    class MyApp : Application() {
    
        override fun onCreate() {
            super.onCreate()
    
            if(BuildConfig.DEBUG){
                Timber.plant(Timber.DebugTree())
            }
        }
    }
    
  3. Add the Application class(MyApp) to the Manifest (AndroidManifest.xml)

    <application
        android:name=".MyApp"
    

Now you can use Timber: Timber.i("Timber logs")

Also can use custom tags if you wish: Timber.tag("Yo").I("used custom tag for logs")

3
  • 1
    Thanks for the answer, literally the one I was searching for!
    – romaneso
    Commented Oct 26, 2020 at 15:07
  • 1
    Adding android:name=".MyApp" to manifest is crucial, thanks! Commented Apr 26, 2021 at 15:10
  • what about using DI (Hilt)? logs can happen earlier - before Timber.plant(Timber.DebugTree()) is called when Hilt creates your dependencies
    – user924
    Commented Apr 6 at 13:16
5

In my case it was wrong BuildConfig import

import org.koin.android.BuildConfig

but my app has

import com.company.example.BuildConfig
1
  • It was import androidx.viewbinding.BuildConfig in my case, thank you!
    – Amr Jyniat
    Commented Aug 18, 2022 at 8:24
2

Probably late to the party but my problem was the my phone was set to "Charge only" and not "file transfer". Apparently I was allowed to build and run, but logs were blocked

EDIT Another solution:

Check your RUN tab in the bottom of Android Studio. Sometimes the logs get output to there instead

1

Check Initialization: Ensure that you've properly initialized Timber in your application. It's usually done in the onCreate method of your Application class.

   class MyApplication : Application() {
       override fun onCreate() {
           super.onCreate()
           if (BuildConfig.DEBUG) {
               Timber.plant(Timber.DebugTree())
           }
       }
   }

Just make sure you are importing BuildConfig from the correct location. The correct way is: should be configured appropriately in your project's Gradle files for example import com.example.appname.BuildConfig. If it still does not work, try import androidx.viewbinding.BuildConfig

Remember to make sure that your app's build configuration is set up correctly in your build.gradle files to ensure that BuildConfig.DEBUG behaves as expected. Debug and release configurations should be configured appropriately in your project's Gradle files.

Ensure that there is no space between the words in the tag. Otherwise, the tag you add to logcat will only display according to the first word. Briefly, space is not counted by logcat.

2
  • what about using DI (Hilt)? logs can happen earlier - before Timber.plant(Timber.DebugTree()) is called when Hilt creates your dependencies
    – user924
    Commented Apr 6 at 13:16
  • @user924, If your main concern is ensuring that Timber is ready to capture logs as soon as possible, go with the early initialization approach. If you have specific requirements for handling logs during DI initialization or need more customization options, consider implementing a custom DebugTree. Commented Apr 11 at 5:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.