Better Logging in Android Using Timber
If you are reading this, you are an Android developer or inspiring to be one. In this blog, we will talk about one of the most widely used libraries, Timber for Android. Timber is used for better logging in Android. As an Android developer, we use a lot of Log statement in our project to generate outputs and we can use it to check if the required output is printed in Terminal or not.
Log are of types like VERBOSE, DEBUG, WARN, INFO, ERROR and we should only limit the use of Log in development (Staging) build and not the production build. This is because we can have a threat to expose some sort of sensitive information.Now, let us see a few ways how can we use the Log in our Android project.
1. First, we create a singleton class,
object LogUtil {
fun DEBUG(key: String, message: String) {
if (BuildConfig.DEBUG)
Log.d(key, message)
}
}
- Here, LogUtil is a Singleton class. It contains a function DEBUG() and it will run only when the app is in debug mode.
To run this in Activity file,
class MainActivity : AppCompatActivity() {
val TAG = MainActivity::class.java.simpleName
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//It will only run in debug mode
LogUtil.DEBUG(TAG,"We are in MainActivity")
}
}
This is one way to use Log in our project.
2. The second way to do is, we use Log directly in our files like,
class MainActivity : AppCompatActivity() {
val TAG = MainActivity::class.java.simpleName
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
Log.d(TAG,"We are in MainActivity")
}
}
In this code snippet we have used the Log directly and to make sure we are not using this in production we remove it using proguard-rules,
-assumenosideeffects class android.util.Log {
public static *** d(...);
public static *** v(...);
public static *** e(...);
}
- Here, we remove Log.d, Log.e and Log.e using the proguard-rules.
The last way to hide Log from Release build, deleting all the Log statement manually.
This can be a tedious task right? Let me take a moment and let me introduce you to Timber .Timber is an open-source library made by Jake Wharton. It simplifies the process of Logging your statements. It will automatically handle your debug/release issue by itself. Let us discuss integrating and using Timber.Add the following in build.gradle,
implementation 'com.jakewharton.timber:timber:4.7.1'
Now, in your application class, we have to Initialize Timber,
class App : Application() {
override fun onCreate() {
super.onCreate()
if (BuildConfig.DEBUG) {
Timber.plant(Timber.DebugTree())
}
}
}
We can also create our own tree by extending Timber.Tree
Now, in your View files replace your Log statement with,
Timber.e("Your Error Message")
Timber.d("Your Debug Message")
Here, you can see we are not passing TAG like above examples as Timber detects the Class name automatically for you.Now, Let us consider an example where we don’t want to pass the class name as the tag and want a custom tag name.For that, we can use,
Timber.tag("Mindorks Tag").e("Error Caught By Mindorks");
That is it, how we can improve our logging skills using Timber.
Happy Coding :)
Team Mindorks.