Skip to main content

How to use Timber for logging in Kotlin Android

How to use Timber for logging in Kotlin Android.

Logging is an important part of any application's development process as it helps developers to debug and monitor their code. Timber is a powerful logging library for Android that provides a simple and flexible logging interface. In this tutorial, we will learn how to use Timber for logging in Kotlin Android.

Step 1: Add Timber Dependency

To use Timber in your Android project, you need to add the Timber dependency to your app-level build.gradle file.

dependencies {
implementation 'com.jakewharton.timber:timber:4.7.1'
}

After adding the dependency, sync your project to download the required files.

Step 2: Initialize Timber

Before you can start using Timber, you need to initialize it in your application's onCreate() method. Open your Application class and add the following code:

import timber.log.Timber

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

Timber.plant(Timber.DebugTree())
}
}

The Timber.plant() method is used to initialize Timber with the DebugTree class, which provides the default Timber logger for debug builds.

Step 3: Start Logging

Now that Timber is initialized, you can start using it for logging in your application. Timber provides several logging methods such as d(), i(), w(), e(), etc. Here's how you can use them:

import timber.log.Timber

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

Timber.d("Debug log")
Timber.i("Info log")
Timber.w("Warning log")
Timber.e("Error log")
}
}

In the above example, we are using Timber's logging methods to log different types of messages. The log messages will be printed in the Logcat with their respective log levels.

Step 4: Customizing Timber

Timber allows you to customize its behavior by creating your own Tree implementation. You can create a custom Tree class by extending the Timber.Tree class and override its logging methods.

Here's an example of a custom Tree class that logs messages to a file:

import timber.log.Timber
import java.io.File
import java.io.FileWriter
import java.io.IOException

class FileLoggingTree(private val file: File) : Timber.Tree() {
override fun log(priority: Int, tag: String?, message: String, t: Throwable?) {
try {
val writer = FileWriter(file, true)
writer.append("$tag: $message\n")
writer.close()
} catch (e: IOException) {
e.printStackTrace()
}
}
}

To use your custom Tree, you need to plant it in the onCreate() method of your Application class:

import timber.log.Timber

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

Timber.plant(FileLoggingTree(File("log.txt")))
}
}

In the above example, we are creating a FileLoggingTree instance with a File object representing the log file. The log messages will be appended to the file each time a log statement is executed.

Step 5: Disable Logging

In some cases, you may want to disable logging in your release builds to improve performance and reduce APK size. You can achieve this by using ProGuard rules.

Open your app-level build.gradle file and add the following ProGuard rule to disable logging:

android {
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}

Create a new file called proguard-rules.pro in your project's root directory (next to the build.gradle file) and add the following rule:

-assumenosideeffects class timber.log.Timber {
public static void d(java.lang.String, java.lang.String);
}

The -assumenosideeffects rule tells ProGuard that the d() method of the Timber class has no side effects and can be safely removed.

Conclusion

In this tutorial, we learned how to use Timber for logging in Kotlin Android. We covered the steps for adding Timber to your project, initializing Timber, logging messages using Timber's logging methods, customizing Timber's behavior with custom Tree implementation, and disabling logging in release builds. Timber provides a convenient and flexible way to log messages in your Android application, making it easier to debug and monitor your code.