Android Better Logging using Timber Library

Timber is a logging utility class based over Android’s Log class. While being developed, we more often than not finish up composing part of log proclamations and before the discharge, we’ll cleanup the log articulations by evacuating them physically (despite the fact that logs can be debilitated in discharge fabricate). This repetitive procedure can be kept away from effectively by utilizing Timber.

Timber gives loads of different alternatives also. How about we perceive how it tends to be utilized in our ventures to keep up the logs better.

1. Timber

The following are not many troubleshoot proclamations printed utilizing default Log class.

int a = 100;

Log.e(“TAG”, String.format(“Integer an esteem is: %d”, a));

String name = “Android Studio”;

Log.e(“TAG”, String.format(“My name is: %s”, name));

The above same articulations can be printed utilizing Timber as beneath.

/whole number

int a = 100;

Timber.d(“Integer an esteem is: %d”, a);

String name = “Android Studio”;

Timber.d(“My name is: %s”, name);

You can see here, the TAG isn’t go to Timber as it consequently distinguishes the class in which logs were composed.

Additionally String formatter isn’t utilized to design the announcement as Timber can do it consequently for you.

2. Coordinating Timber

Presently how about we perceive how to incorporate Timber library in your task making it accessible in each class.

1. Make another undertaking in Android Studio from File ⇒ New Project and select Basic Activity from formats.

2. Open build.gradle and include Timber reliance. Butterknife is discretionary here, however required for this precedent.

/timber

usage ‘com.jakewharton.timber:timber:4.7.1’

/margarine blade

execution ‘com.jakewharton:butterknife:8.8.1’

annotationProcessor ‘com.jakewharton:butterknife-compiler:8.8.1’

3. Timber must be instated when application begins. Thus, Application class would be best spot to do that. Make new class named MyApplication.java and expand the class from Application.

Introduce Timber in onCreate strategy by planting another Tree.

Here Timber.DebugTree() prints signs in troubleshoot mode.

In the event that you need to get special cases in discharge mode, you can make an alternate Tree and plant it in discharge mode. This progression is totally discretionary yet in the event that you need to send exemptions to an alternate administration, this is the suitable spot to do it.

bundle info.androidhive.timber;

import android.app.Application;

import info.androidhive.timber.log.ReleaseTree;

import timber.log.Timber;

open class MyApplication expands Application {

@Override

open void onCreate() {

super.onCreate();

in the event that (BuildConfig.DEBUG) {

Timber.plant(new Timber.DebugTree());

} else {

Timber.plant(new ReleaseTree());

}

}

}

4. You can make a custom tree by expanding the class from Timber.Tree. Here is the case of ReleaseTree.java class.

bundle info.androidhive.timber.log;

import android.util.Log;

import timber.log.Timber;

open class ReleaseTree expands Timber.Tree {

@Override

secured void log(int need, String tag, String message, Throwable t) {

in the event that (need == Log.VERBOSE || need == Log.DEBUG) {

return;

}

/log your collide with your top choice

/Sending crash report to Firebase CrashAnalytics

/FirebaseCrash.report(message);

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

}

}

4. At long last add MyApplication to your <application> tag in your AndroidManifest.xml

<?xml version=”1.0″ encoding=”utf-8″?>

<manifest xmlns:android=”http://schemas.android.com/apk/res/android”

package=”info.androidhive.timber”>

<application

android:name=”.MyApplication”

…>

</application>

</manifest>

5. Presently, Timber is prepared to use in your application. The following are not many instances of Timber log explanations showing distinctive situations.

bundle info.androidhive.timber;

import android.os.Bundle;

import android.support.v7.app.AppCompatActivity;

import android.support.v7.widget.Toolbar;

import android.widget.Toast;

import butterknife.ButterKnife;

import butterknife.OnClick;

import butterknife.Unbinder;

import timber.log.Timber;

open class MainActivity broadens AppCompatActivity {

private unbinder;

@Override

ensured void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

unbinder = ButterKnife.bind(this);

Toolbar = findViewById(R.id.toolbar);

setSupportActionBar(toolbar);

/boolean

boolean isWeekend = false;

Timber.d(“This prints the boolean esteem. Is weekend: %b”, isWeekend);

/number

int a = 100;

Timber.d(“Integer an esteem is: %d”, a);

/skim

drift pi = 3.14159f;

Timber.d(“Pi esteem is: %f”, pi);

}

@OnClick(R.id.btn_log_string)

void logMessage() {

Timber.d(“Hello from Timber!”);

showToast();

}

@OnClick(R.id.btn_log_exception)

void logException() {

attempt {

int a = 10/0;

Timber.d(“Value of a: %d”, a);

} get (Exception e) {

Timber.e(e);

/or/

Timber.e(“Exception in math activity: %s”, e.getMessage());

}

showToast();

}

private void showToast() {

Toast.makeText(getApplicationContext(), “Check LogCat for message or blunder!”, Toast.LENGTH_SHORT).show();

}

@Override

secured void onDestroy() {

super.onDestroy();

unbinder.unbind();

}

}

Leave a Reply

Your email address will not be published. Required fields are marked *