As per the official GitHub repository of Timber : “Timber is a logger with a small, extensible API which provides utility on top of Android’s normal Log class”. It is a flexible logging library for Android which makes logging a lot more convenient. In this blog you will learn how to use Timber in SUSI.AI Android app.
To begin, add Timber to your build.gradle and refresh your gradle dependencies.
Two easy steps to use Timber:
- Install any Tree instances that you want in the onCreate() of your application class.
- Call Timber’s static methods everywhere throughout the app.
You can add the following code to your application class :
Timber ships with a ‘Debug Tree’ that provides all the basic facilities that you are very used to in the common Android.log framework. Timber has all the logging levels that are used in the normal Android logging framework which are as follows:
- Log.e: Use this tag in places like inside a catch statement where you are aware that an error has occurred and therefore you’re logging an error.
- Log.w: Use this to log stuff you didn’t expect to happen but isn’t necessarily an error.
- Log.i: Use this to post useful information to the log. For instance, a message that you have successfully connected to a server.
- Log.d: Use this for debugging purposes. For instance, if you want to print out a bunch of messages so that you can log the exact flow of your program or if you want to keep a log of variable values.
- Log.v: Use this if, for some reason, you need to log every little thing in a particular part of your app.
- Log.wtf: Use this when you encounter a terrible failure.
- You can use Timber.e, Timber.w, Timber.i, Timber.d and Timber.v respectively for the logging levels mentioned above. However, there is a small difference. In Android logging framework the exception is passed as the last parameter. But, when using Timber, you need to provide the exception as the first parameter. Interestingly, while using Timber you don’t have to provide a TAG in logging calls because it automatically does that for you. It uses the file name, where you are logging from, as the TAG. To log an exception you can simply write:
- Or a null message if you want:
- A simple message can be logged sent via Crashlytics too:
Did you notice? There is no TAG parameter. It is automatically assigned as the caller class’ name. Now, you can use Timber for logging in SUSI Android app.
- Official repository JakeWharton/Timber:
- Blog post on implementation of Timber :