Customize your Android Timber logger setup to add a global tag and a method name to the logs for easy debugging.
Table of Contents
- Getting started
- Global Tag prefix to Timber logs
- Adding method name to Timber logs
- References
Customize your Android Timber logger setup to add a global tag and a method name to the logs for easy debugging blog post aims to provide code for a customized Android Timber logger setup that could help a lot in debugging.
Code
https://github.com/androiddevnotesyoutube/custom-android-timber-logger-setup
Getting started
To add Timber to an Android project, add this dependency in your app-level build.gradle in the dependencies
section:
You can find the latest versions for Timber on the official GitHub repo.
Create an Application
class
Create an App.kt Application class and add it to the AndroidManifest.xml
App.kt could be named anything and if you already have an Application
class, you do not need to create another Application
class.
Installation of Timber should be done as early as possible as recommended by the Timber Author. We will initialize Timber in the onCreate
of the App.kt which extends Application
class.
The basic setup is done.
Global Tag prefix to Timber logs
By default, Timber automatically figures out from which class it's being called and adds that class as its tag
.
You can see the basic default sample by the Timber Author here.
This section will show you how to add the “global tag” prefix to Android Timber logs, so it’s easy to filter Timber logs from multiple classes with a single search query.
As seen in the image, the search query “global_tag_” can filter Timber logs from multiple classes which is useful when you want to see logs from all classes at the same time.
Adding method name to Timber logs
Some classes have variables named exactly the same but in different methods. When you log such variables at the same time, it’s possible to get confused to figure out from which method they are coming.
To fix that, in this section we will add a method name
to our tag, so it's easy to see the variable from which method is being logged.
As seen in the image, the “method name” is added to the tag!
Similarly, you can also do the element.lineNumber
to get the line number.
Video Tutorial for Android Timber: https://www.youtube.com/watch?v=mtrmk_9YwEc
Code: https://github.com/androiddevnotesyoutube/custom-android-timber-logger-setup
References
https://github.com/JakeWharton/timber
Find us at
Discord: https://discord.gg/7BgVnSb3SF
GitHub: https://github.com/androiddevnotes/
YouTube: https://www.youtube.com/channel/UCQATLaT0xKkSm-KKVQzpu0Q
Twitter: https://twitter.com/androiddevnotes
Instagram: https://www.instagram.com/androiddevnotes
Contributions
Contributions and Pull requests are welcomed at https://github.com/androiddevnotes repositories!
🐣