Android Better Logging -Gossip Buzzer

Lumber is a logging utility class based on head of Android’s Log class. While being developed, we typically wind up composing parcel of log explanations and before the discharge, we’ll cleanup the log articulations by expelling them physically (despite the fact that logs can be handicapped in discharge manufacture). This dreary procedure can be kept away from effectively by utilizing Timber.

Lumber gives heaps of different alternatives also. How about we perceive how it very well may be utilized in our activities to keep up the logs better.

1. Lumber

The following are not many investigate explanations printed utilizing default Log class.

int a = 100;

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

String name = “Android Studio”;

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

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

/whole number

int a = 100;

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

String name = “Android Studio”;

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

You can see here, the TAG isn’t passed to Timber as it consequently identifies the class where logs were composed.

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

2. Incorporating Timber

Presently how about we perceive how to incorporate Timber library in your undertaking 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, yet required for this model.

/wood

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

/spread blade

execution ‘com.jakewharton:butterknife:8.8.1’

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

3. Wood must be instated when application begins. Along these lines, 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 technique by planting another Tree.

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

On the off chance that you need to get exemptions in discharge mode, you can make an alternate Tree and plant it in discharge mode. This progression is totally discretionary however on the off chance that you need to send special cases to an alternate help, 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 broadens Application {

@Override

open void onCreate() {

super.onCreate();

on the off chance 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 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 barely any instances of Timber log proclamations exhibiting various 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 expands AppCompatActivity {

private unbinder;

@Override

secured 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 = bogus;

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

/whole number

int a = 100;

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

/coast

coast 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 mistake!”, Toast.LENGTH_SHORT).show();

}

@Override

ensured void onDestroy() {

super.onDestroy();

unbinder.unbind();

}

}

Leave a Reply

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