DEV Community

Cover image for Effortless Android Logging with Timber and Kotlin
supriya shah
supriya shah

Posted on

Effortless Android Logging with Timber and Kotlin

Originally published on Medium:
https://medium.com/@supsabhi/effortless-android-logging-with-timber-and-kotlin-f0aaa0a701b7

Logging is a cornerstone of Android development. It helps you track down bugs, monitor app behavior, and understand what’s happening under the hood-both during development and after your app is live. In short, logging is your best friend when coding!

But let’s be honest: Android’s built-in Log class, while functional, can get unwieldy in bigger projects. Managing log tags, filtering output, and cleaning up before release can quickly become a headache.

That’s where Timber comes in-a lightweight, open-source logging library created by Jake Wharton that makes logging in Android (especially with Kotlin) much more pleasant.

What Makes Timber Special?

Concise and Clean: Timber’s API is simple and removes the need for repetitive boilerplate code.
Automatic Tag Generation: No more hardcoded tags-Timber automatically uses your class name as the tag
Customizable Trees: Timber introduces the concept of “trees,” which let you control how and where logs are output. For example, you can send logs to crash reporting tools or filter them by build type.
Production Safety: By default, Timber encourages you to disable logs in release builds, helping prevent sensitive information from leaking or cluttering production logs
Setting Up Timber in Your Android App

Step 1: Add the Dependency

In your app-level build.gradle:

implementation 'com.jakewharton.timber:timber:5.0.1'
Enter fullscreen mode Exit fullscreen mode

(Check for the latest version before adding.)

Step 2: Initialize Timber in Your Application Class

The best place to “plant” your Timber trees is in your custom Application class. This ensures logging is set up as soon as your app starts.

class MyApp : Application() {
override fun onCreate() {
super.onCreate()
if (BuildConfig.DEBUG) {
Timber.plant(Timber.DebugTree())
} else {
Timber.plant(ReleaseTree()) // Custom tree for release
}
}
}
Enter fullscreen mode Exit fullscreen mode

DebugTree automatically tags logs with the calling class name, making debugging easier.
For production, you can implement a custom ReleaseTree to filter or redirect logs as needed.
Don’t forget to declare your Application class in the AndroidManifest.xml:

<application
android:name=".MyApp"
…>
</application>
Enter fullscreen mode Exit fullscreen mode

Step 3: Logging in Your Code

Replace all usages of Log.d, Log.e, etc., with Timber’s methods:

Timber.d("Debug message")
Timber.i("Info message")
Timber.w("Warning message")
Timber.e("Error message: %s", error)
You can use string templates for dynamic content: Timber.d(“User id: $userId”).
Enter fullscreen mode Exit fullscreen mode

All standard log levels (verbose, debug, info, warn, error) are supported.
Advanced Usage: Custom Trees and Extensions

Custom Trees

Timber’s tree system lets you define how logs are handled. For example, you can create a tree that sends errors to Firebase Crashlytics or Sentry:

class CrashReportingTree : Timber.Tree() {
override fun log(priority: Int, tag: String?, message: String, t: Throwable?) {
if (priority >= Log.WARN) {
// Send to crash reporting
}
}
}
Enter fullscreen mode Exit fullscreen mode

Plant your custom tree in release builds to capture only important logs.

Kotlin Extensions

Libraries like timberkt provide Kotlin-specific extensions, making logging even more idiomatic:

Timber.d { "User id: $userId, name: ${user.name}" }

Enter fullscreen mode Exit fullscreen mode

The lambda is only evaluated if the log will actually be printed, improving performance.
Why Choose Timber?

No More Manual Tags: Timber generates tags for you, so you don’t have to manage them manually.
Easy Cleanup: You can disable logging in release builds with a single line, so you never have to hunt down and remove log statements by hand.
Customizable Behavior: In production, you can route logs to crash reporting services instead of Logcat, ensuring you only capture what matters most.
Automatic Metadata: Timber can include class names, line numbers, and more, making your logs more informative.
Final Thoughts

Timber isn’t just another logging library-it’s a thoughtful enhancement to your Android development workflow. With concise syntax, automatic tagging, and customizable trees, Timber makes logging a breeze without sacrificing control or safety.

So next time you’re about to write Log.d(“TAG”, “Some message”), give Timber a try. It might just change the way you log forever.

Happy Coding!

Top comments (0)