I heard about Timber and was reading github README
, but it's quietly confusing me.
Behavior is added through Tree instances. You can install an instance by calling Timber.plant. Installation of Trees should be done as early as possible. The onCreate of your application is the most logical choice.
What behavior?
This is a logger with a small, extensible API which provides utility on top of Android's normal Log class.
What more does it provide on top of Android's Log?
The DebugTree implementation will automatically figure out from which class it's being called and use that class name as its tag. Since the tags vary, it works really well when coupled with a log reader like Pidcat.
What is DebugTree?
There are no Tree implementations installed by default because every time you log in production, a puppy dies.
Again, what is a tree implementation? What does it do? And how do I stop killing puppies?
Two easy steps:
Install any Tree instances you want in the onCreate of your application class.
Call Timber's static methods everywhere throughout your app.
Two easy steps for accomplishing what?
None of this has been explained in the Readme. It's pretty much a description for someone who already knows what it is :/
Timber is an open-source library made by Jake Wharton. To add its dependency open build.gradle file and add – implementation 'com.jakewharton.timber:timber:4.7.1' and sync project. Step 2: Make Application Class.
Timber is — with few words — an API for Android's Log class. It basically enhances the logs from Android. We do that by planting a Tree, and each time we log something, the behavior may change depending on which Tree implementation end up been called.
Logging, or commercial logging, involves cutting trees for sale as timber or pulp. The timber is used to build homes, furniture, etc and the pulp is used to make paper and paper products. Logging is generally categorized into two categories: selective and clear-cutting.
Timber is a logger with a small, extensible API which provides utility on top of Android's normal Log class. In the library Behavior is added through Tree instances. You can install an instance by calling Timber. plant . Installation of Tree s should be done as early as possible.
Problem :-
We do not want to print logs in Signed application as we may sometimes log sensible information . Generally to overcome this developers tend to write if condition before writing log
Example:-
if(BuildConfig.DEBUG) {
Log.d(TAG,userName);
}
so every time you want to print a log you need to write a if condition and a TAG which most times will be class name
Timber tackels these two problems
You just need to check condition once in application class and initialize Timber.plant
class MyApplication : Application() {
override fun onCreate() {
super.onCreate()
if (BuildConfig.DEBUG) {
Timber.plant(DebugTree())
}
}
}
remaining all places we can just write Timber.d("Message")
without any tag or if condition .
If you want a different tag then you can use
Timber.tag("Tag").d("message");
Edit :
Also you can plant your own trees in Timber , and do some cool stuff like Log only warnings and error in release , send warnings and error logs to server etc . eg
import timber.log.Timber;
public class ReleaseTree extends Timber.Tree {
@Override
protected void log(int priority, String tag, String message, Throwable t) {
if (priority == ERROR || priority == WARNING){
//Send to server
}
}
}
You can plant different trees for different build flavours .
Check out this article and have a listen to this podcast
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With