Timber provides many helpful options for higher logging. Let’s see how we will use it in our tasks to maintain logs organized.
1. Timber
Beneath are the few debug statements which are printed utilizing default Log class.
val a = 100
Log.e(“TAG”, String.format(“Integer a worth is: %d”, a))
val title = “Android Studio”
Log.e(“TAG”, String.format(“My title is: %s”, title))
The identical statements might be printed utilizing Timber as beneath.
// integer
val a = 100
Timber.d(“Integer a worth is: %d”, a)
val title = “Android Studio”
Timber.d(“My title is: %s”, title)
You possibly can discover right here, the TAG will not be handed to Timber because it robotically detects the category during which logs had been written.
Additionally, the String formatter will not be used to format the assertion as Timber can do it robotically for you.
2. Integrating Timber
Now let’s examine easy methods to combine the library in your venture making it accessible in each class.
Create a brand new venture in Android Studio from File ⇒ New Challenge and choose Empty Actions from templates.
Open construct.gradle and add Timber dependency.
implementation ‘com.jakewharton.timber:timber:5.0.1’
Timber must be initialized as quickly as app begins. So, Utility class can be greatest place to try this. Create new class named MyApplication.kt and prolong the category from Utility.
Initialize Timber in onCreate methodology by planting a brand new Tree.
Use Timber.DebugTree() to print the logs solely in debug mode.
If you wish to catch exceptions in launch mode, you possibly can create a unique Tree and plant it in launch mode. This step is totally elective however if you wish to ship exceptions to a unique service, that is the suitable place to do it.
bundle information.androidhive.android_timber
import android.app.Utility
import android.util.Log
import timber.log.Timber
class MyApplication : Utility() {
override enjoyable onCreate() {
tremendous.onCreate()
if (BuildConfig.DEBUG) {
Timber.plant(Timber.DebugTree())
} else {
Timber.plant(ReleaseTree())
}
}
non-public class ReleaseTree : Timber.Tree() {
override enjoyable log(precedence: Int, tag: String?, message: String, t: Throwable?) {
if (precedence == Log.VERBOSE || precedence == Log.DEBUG) {
return
}
// log your crash to your favorite
// Sending crash report back to Firebase CrashAnalytics
// FirebaseCrash.report(message);
// FirebaseCrash.report(new Exception(message));
}
}
}
Do not forget so as to add MyApplication to your <utility> tag in your AndroidManifest.xml
<utility
….
android:title=”.MyApplication”
Now, Timber is prepared for use in your app. Beneath are the few examples of Timber log statements demonstrating totally different eventualities.
bundle information.androidhive.android_timber
import android.os.Bundle
import androidx.exercise.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import timber.log.Timber
@Suppress(“DIVISION_BY_ZERO”)
class MainActivity : AppCompatActivity() {
override enjoyable onCreate(savedInstanceState: Bundle?) {
tremendous.onCreate(savedInstanceState)
enableEdgeToEdge()
setContentView(R.structure.activity_main)
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.major)) { v, insets ->
val systemBars = insets.getInsets(WindowInsetsCompat.Kind.systemBars())
v.setPadding(systemBars.left, systemBars.prime, systemBars.proper, systemBars.backside)
insets
}
Timber.d(“Hey from Timber!”)
// boolean
val isWeekend = false
Timber.d(“This prints the boolean worth. Is weekend: %b”, isWeekend)
// integer
val a = 100
Timber.d(“Integer a worth is: %d”, a)
// float
val pi = 3.14159f
Timber.d(“Pi worth is: %f”, pi)
strive {
val ans = 10 / 0
Timber.d(“Worth of a: %d”, ans)
} catch (e: Exception) {
Timber.e(e)
// or
Timber.e(“Exception in math operation: %s”, e.message)
}
}
}
Let me know your queries within the feedback part beneath.
Cheers!Comfortable Coding 🤗