How to tell Timber which log call is for which Tree?

2k views Asked by At

I have Timber library for logging and cooperation with crash-reporting services and I have both Crashlytics and Loggly services in my app.

Thus, I had to plant two trees:

Timber.plant(new CrashlyticsTree());
Timber.plant(new LogglyTree(BuildConfig.LOGGLY_TOKEN));

Now, each time I call:

Timber.e("bla bla");

I get all the logs in Loggly, but I want some of them to go to Loggly and some of them to go to Crashlytics, so how do I do that?

1

There are 1 answers

0
Kaloyan Roussev On BEST ANSWER

Turns out every call to .e or .w for example, iterates through all planted trees and calls their respective .e and .w implementations.

This means that if I wanted to separate Library A and Library B I needed to use different logging priority for each.

So I chose to use .e for Library A and .w for Library B.

In order to do that, I had to create custom trees that inherit from Timber.HollowTree and only implement the needed log call, and leave the rest of them hollow.

public class LibraryATree extends Timber.HollowTree {
    @Override
    public void e(Args){
        // Do something
    }
}

public class LibraryBTree extends Timber.HollowTree {
    @Override
    public void w(Args){
        // Do something
    }
}

Timber.plant(new LibraryATree())
Timber.plant(new LibraryBTree())

Now in my code, if I want to log something via LibraryA, I do this:

Timber.e("Test Library A"); // calls LibraryA's Tree's `.e` method

and if I wanted to use Library B's logging utilities I do this:

Timber.w("Test Library B"); // calls LibraryB's Tree's `.w` method