Tuesday, October 12, 2021

Square Logcat implementation in Java

There is no any steps for java implementation and some java developer want's to implement this library so here is the steps

Add Dependency in your gradle file

implementation 'com.squareup.logcat:logcat:0.1' 

First step 

Install AndroidLogcatLogger in Application's onCreate() method

import android.app.Application;
import logcat.AndroidLogcatLogger;
import logcat.LogPriority;

public class ExampleApplication extends Application {
  @Override
  public void onCreate() {
    super.onCreate();
    AndroidLogcatLogger.Companion.installOnDebuggableApp(this,  LogPriority.VERBOSE);
  }
}

Now you can use this library in your any class

 
LogcatKt.logcat("MyFirstTag", LogPriority.ERROR, () -> "Add your log here");

Or if we want to print any exception then try below code.
   
LogcatKt.logcat("MyOtherTag", LogPriority.ERROR, () -> ThrowablesKt.asLog(new RuntimeException("Hello world")));

If Square developer want's then they can provide good java implementation also I have create one PR for it to easily java implementation.

Here is the github link of this library : https://github.com/square/logcat

Best of luck...

No comments:

Post a Comment