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()

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Ethan_AI
  • 1,049
  • 2
  • 13
  • 20

4 Answers4

36

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());
Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68
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")

AFA
  • 1,009
  • 11
  • 8
4

In my case it was wrong BuildConfig import

import org.koin.android.BuildConfig

but my app has

import com.company.example.BuildConfig
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

MikkelT
  • 633
  • 1
  • 10
  • 16