Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Timber Not Logging in Kotlin Android

Tags:

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

like image 411
Ethan_AI Avatar asked Jul 08 '18 20:07

Ethan_AI


2 Answers

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()); 
like image 103
Pavneet_Singh Avatar answered Sep 18 '22 17:09

Pavneet_Singh


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

like image 23
AFA Avatar answered Sep 18 '22 17:09

AFA