Skip to main content

How to log exceptions with Timber in Kotlin Android

How to log exceptions with Timber in Kotlin Android.

Logging exceptions is an essential part of debugging and understanding the behavior of your Android application. Timber is a popular logging library that provides a simple and flexible API for logging in Android applications. In this tutorial, we will learn how to log exceptions using Timber in Kotlin Android.

Step 1: Add Timber Dependency

To get started, you need to add the Timber dependency to your project. Open your app-level build.gradle file and add the following line in the dependencies block:

implementation 'com.jakewharton.timber:timber:4.7.1'

After adding the dependency, sync your project to download the Timber library.

Step 2: Initialize Timber

Before you can start logging exceptions with Timber, you need to initialize it in your application class or wherever you want to use it. Create a new Kotlin file called MyApplication.kt (or use an existing one) and add the following code:

import android.app.Application
import timber.log.Timber

class MyApplication : Application() {
override fun onCreate() {
super.onCreate()

if (BuildConfig.DEBUG) {
Timber.plant(Timber.DebugTree())
} else {
// For release builds, you can plant your own custom tree
// e.g., Timber.plant(CrashlyticsTree())
}
}
}

In the above code, we check if the app is running in debug mode (BuildConfig.DEBUG) and plant the Timber.DebugTree for debug builds. For release builds, you can plant your own custom tree, such as CrashlyticsTree for logging exceptions to Crashlytics.

Don't forget to update your AndroidManifest.xml file to use this custom application class. Add the android:name attribute to the <application> element:

<application
android:name=".MyApplication"
...
</application>

Step 3: Log Exceptions

Once Timber is initialized, you can start logging exceptions. Here are a few examples of how to log exceptions using Timber:

Example 1: Log Exception and Message

try {
// Some code that may throw an exception
} catch (e: Exception) {
Timber.e(e, "An error occurred")
}

In the above code, we catch an exception and log it using Timber.e(). The first parameter is the exception (e), and the second parameter is an optional message.

Example 2: Log Exception Only

try {
// Some code that may throw an exception
} catch (e: Exception) {
Timber.e(e)
}

In this example, we log the exception without any additional message.

Example 3: Log Exception with Custom Tag

try {
// Some code that may throw an exception
} catch (e: Exception) {
Timber.tag("MyTag").e(e, "An error occurred")
}

In this example, we log the exception with a custom tag (MyTag). This can be useful for filtering and organizing logs.

Example 4: Log Exception with Custom Priority

try {
// Some code that may throw an exception
} catch (e: Exception) {
Timber.wtf(e, "An error occurred")
}

In this example, we log the exception with a custom priority (Timber.wtf). This priority is used for logging severe errors that may cause the application to crash.

Conclusion

In this tutorial, we learned how to log exceptions using Timber in Kotlin Android. By following the steps outlined above, you can easily integrate Timber into your Android application and log exceptions with ease. Logging exceptions can help you identify and fix issues in your app, providing a better user experience.