Android Development

Android Higher Logging with Timber Library

Timber is a category of logging utilities constructed on the logging class of Android. Throughout growth, we normally find yourself writing many log directions, and earlier than publishing, we clear them up by deleting them manually (regardless that logs might be disabled when creating the model). This tedious course of might be simply prevented by utilizing Timber.

The wooden offers many different choices. Let's see how it may be utilized in our tasks to raised handle the logs.

1. wooden

Beneath are some debug directions printed utilizing the default log class.

int a = 100;
Log.e ("TAG", String.format ("An integer worth is:% d", a));

Channel title = "Android Studio";
Log.e ("TAG", String.format ("My title is:% s", title));

The identical statements might be printed utilizing Timber as beneath.

// integer
int a = 100;
Timber.d ("Integer a worth is:% d", a);

Channel title = "Android Studio";
Timber.d ("My title is:% s", title);

You possibly can discover right here, the LABEL It’s not handed to Timber as a result of it robotically detects the category wherein the logs have been written.
Additionally Chain coach It’s not used to format the instruction as a result of Timber can do it robotically for you.

2. Integration of wooden

Now let's see find out how to combine the Timber library into your venture and make it out there in all lessons.

1. Create a brand new venture in Android Studio from File ⇒ New venture and choose Primary exercise from fashions.

2. Open construct.gradle and add wooden habit. Butterknife is non-compulsory right here, however required for this instance.

// wooden
implementation & # 39; com.jakewharton.timber: woodwork: four.7.1 & # 39;

// butter knife
implementation: com.jakewharton: butterknife: eight.eight.1 & # 39;
annotationProcessor & com.jakewharton: butterknife-compiler: eight.eight.1 & # 39;

three. The wooden have to be initialized as quickly as the appliance begins. The applying class could be the very best place to do it. Create a brand new named class MonApplication.java and increase the category of Utility.

Initialize Timber within the onCreate methodology by planting a brand new tree.
Proper right here Timber.DebugTree () shows the logs in debug mode.
If you wish to catch exceptions in publishing mode, you’ll be able to create one other tree and plant it in publishing mode. This step is completely non-compulsory, however if you wish to ship exceptions to a special service, it’s the acceptable place to do it.

bundle data.androidhive.timber;

import android.app.Utility;

import data.androidhive.timber.log.ReleaseTree;
import timber.log.Timber;

Public class MyApplication extends Utility
@Override
public void onCreate ()

four. You possibly can create a customized tree by extending the category of Timber.Tree. Right here is the instance of ReleaseTree.java class.

bundle data.androidhive.timber.log;

import android.util.Log;

import timber.log.Timber;

Public class ReleaseTree extends Timber.Tree
@Override
protected void log (int precedence, String tag, String message, Throwable t) precedence == Log.DEBUG)

// join your crash to your favourite
// Ship the incident report back to Firebase CrashAnalytics

// FirebaseCrash.report (message);
// FirebaseCrash.report (new Exception (message));

four. Lastly add My utility to your tag in your AndroidManifest.xml


5. Timber is now prepared to be used in your utility. Beneath are some examples of Timber Log directions with completely different eventualities.

bundle data.androidhive.timber;

import android.os.Bundle;
import android.help.v7.app.AppCompatActivity;
import android.help.v7.widget.Toolbar;
import android.widget.Toast;

import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import timber.log.Timber;

Public class MainActivity extends AppCompatActivity

Hello! I’m founding father of androidhive and passionate programming. My expertise embody Android, iOS, PHP, Ruby on Rails and rather more. When you’ve got an concept that you prefer to me to develop? Let's discuss ravi@androidhive.data