How it works — Inside Timber!

Farhan Patel
MindOrks
Published in
2 min readOct 7, 2019

We use all sorts of different libraries in our day to day development, Often(rightly!) giving a pass to understanding how it actually works. But getting inside the libraries pays off by exposing all the concepts & little tricks which we are not aware of yet, adding them to our arsenal to be used in our daily battles.

Logging is a great and easy way to debug what’s happening inside your application. Android provides Log class to facilitate logging, but the annoying part is the TAG that needs to provide on every log statement you write. While this can be simplified by creating constants, how about just avoiding it?.

This is where Timber comes in.

I’ve been using Timber for a while now, always wondering how Jake Wharton managed to fetch that class name! so I decided to find it out myself.

After cloning the library for Github, I was kind of surprised to find out the whole library is essentially just ONE class.

Yeah, let that sink.

As stated on Github, Timber acts as a wrapper on top of the android provided Log class.

Now coming back to how it manages to fetch the class name...

And the answer is…

StackTrace.

A stack trace is a list of the method calls that the application was in the middle of when an uncaught exception was thrown.

Traditionally, we use StackTrace to debug errors and bugs. Jake as always very smartly used it solve the TAG problem.

Following getTag() method creates a new Throwable object and gets the StackTrace array.

6th element(namely CALL_STACK_INDEX) in the array is the class in which we created the log(Timber.d(“mind blown”);) then it’s just a leg work to retrieve the class name.

So that’s it! Pretty straight forward right?, Yeah.

Happy Logging!!

--

--