Ad

What Exactly Does The Timber Library Do?

- 1 answer

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 :/

Ad

Answer

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

Ad
source: stackoverflow.com
Ad