Timber for Kotlin: Complete Guide with Examples
1. What It Is and Why You Need It
Timber is a lightweight logging library for Android apps, written in Kotlin. It was created by Jake Wharton — one of the leading Android developers, known for projects like Retrofit, OkHttp, and Dagger. Timber's main goal is to eliminate boilerplate logging code and provide a flexible mechanism for managing logs across different development stages.
Why use Timber when you already have the standard android.util.Log? First, Timber automatically adds the class and method name to every log, making debugging much easier. You no longer need to write Log.d(TAG, "message") — just call Timber.d("message"). Second, Timber provides a "Tree" system that lets you route logs to different channels: for example, output everything to Logcat in debug builds, but only send errors to Crashlytics or another analytics service in release builds.
A third major advantage: Timber automatically disables all logs in release builds unless you've set up a custom production tree. This not only boosts performance but also prevents sensitive data leaks through logs. The library is so popular that it's used in thousands of commercial projects and is recommended by Google in official Android development guides.
2. Installation
Timber is distributed via Maven Central. To install it, add the dependency to your build.gradle file (module level):
dependencies {
implementation 'com.jakewharton.timber:timber:5.0.1'
}
If you're using Kotlin DSL (build.gradle.kts):
dependencies {
implementation("com.jakewharton.timber:timber:5.0.1")
}
After syncing Gradle, the library is ready to use. Note: Timber does not require any additional permissions in AndroidManifest.xml.
3. Quick Start — Minimal Working Example
To get started, you need to "plant a tree" — register a log handler. The simplest way is to use the built-in Timber.DebugTree, which outputs logs to Logcat at DEBUG level and above:
import timber.log.Timber
class MyApplication : Application() {
override fun onCreate() {
super.onCreate()
if (BuildConfig.DEBUG) {
Timber.plant(Timber.DebugTree())
}
}
}
Now you can log from anywhere in your app:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
Timber.d("Activity created")
Timber.i("User opened main screen")
Timber.w("Something suspicious happened")
Timber.e("Error occurred: %s", "Network timeout")
}
}
In Logcat, you'll see something like this:
D/MainActivity: Activity created
I/MainActivity: User opened main screen
W/MainActivity: Something suspicious happened
E/MainActivity: Error occurred: Network timeout
Notice: Timber automatically detected the class name (MainActivity) and added it to the tag. You don't need to manually create a TAG constant.
4. Key Methods/Functions/Classes
4.1. Timber.d() — Debug Logging
Signature: fun d(message: String, vararg args: Any?)
What it does: Outputs a debug message at the DEBUG level. Used for temporary logs during development. By default, it's not shown in release builds.
Timber.