Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What exactly does the Timber library do?

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

like image 956
Mahdi Moqadasi Avatar asked Dec 24 '18 11:12

Mahdi Moqadasi


People also ask

What is timber library?

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.

What is timber library in Android?

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.

What is logging used for?

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.

What is timber Kotlin?

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.


1 Answers

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

like image 101
Manohar Avatar answered Oct 03 '22 16:10

Manohar