Timber: Simple logging API for android

Rashi Karanpuria
Apr 26, 2017 · 1 min read

I hope I don’t need to tell you, why logging is an essential part of software development. It provides developers and support teams with special glasses which enable them to see what the application code is really doing.

A lot of bugs can be avoided directly by writing log statements.

So, I am tired of creating a LOG_TAG in every android component and calling Log.d(LOG_TAG, “this and that with” + this);

Thanks to Jake Wharton’s library Timber, logging is now extremely simple and you can directly log for crash reporting too.

Step 1: Import lib:

compile ‘com.jakewharton.timber:timber:4.5.1

Step 2: Add default tree to application’s OnCreate method:

@Override
public void onCreate() {
super.onCreate();
if (BuildConfig.DEBUG) {
Timber.uprootAll();
Timber.plant(new Timber.DebugTree());
}
else Timber.plant(new FirebaseTree());
}

private static class FirebaseTree extends Timber.Tree {
@Override
protected void log(int priority, String tag, String message, Throwable t) {
if (priority == Log.VERBOSE || priority == Log.DEBUG) {
return;
}

FirebaseCrash.log(message);

if (t != null) {
FirebaseCrash.report(t);
}
}
}

Note that, this methodology avoids making log statements when app is in production. Don’t even get me started on why you should not log in production, a penguin dies when you do that. Here I am logging all error on Firebase, just as I need.

Step 3: Start having fun with logging:

Timber.d("Activity Created");
Timber.i("A button with ID %s was clicked to say '%s'.", button.getId(), button.getText());

Find out more: Library source

Please add your thoughts or point out mistakes in comments.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store