Timber: Simple logging API for android

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.