In this article I show you how to use timber for logging in your Android App.
Timber is built upon Android’s Log class. If you are not yet familiar with logging in Android have a look at Logging in Android
Dependency
Timber has just on dependency. Nice!
dependencies {
implementation 'com.jakewharton.timber:timber:5.0.1'
}
Initialization in Application
To use Timber in your app you need an application class where you call the static method Timber.plant
import android.app.Application
import timber.log.Timber
class LevelUpApplication : Application() {
override fun onCreate() {
super.onCreate()
if (BuildConfig.DEBUG) {
Timber.plant(DebugTree())
}
}
}
Make sure that your custom application class is referenced in your manifest:
<application android:name=".LevelUpApplication" ... >
Import & Logging
After you have added Timber Android Studio highlights the old log statements:
Timber is imported via
import timber.log.Timber
And the new log statement looks like this:
Timber.d("onCreate")
The new log statement has no TAG element so you can remove the companion object from your class.