Logger Basic Usage

Posted by thomas2 on Fri, 17 May 2019 18:14:59 +0200

Logger Basic Usage

brief introduction

Simple, pretty and powerful logger for android
Simple, powerful and beautifully formatted tools for Android

The essence is to encapsulate the Log class provided by the system, and add some partitioning lines to make it easy to find different logs; the information displayed in logcat is configurable. The original look is as follows


Contains thread information, the class, method, and the number of rows logs are in.

I couldn't help but write my favorite function, Gagaga.~~
The most basic dependencies and simple printing on page 2

I think the best feature is that Logger supports setting logs to be saved locally so that you can upload them. Do your own log management system!
But the location of the log was written to death. The way to locate is
The constructor of the DiskLogAdapter class in the Logger package goes into the build() method.

public DiskLogAdapter() {
    formatStrategy = CsvFormatStrategy.newBuilder().build();
}

Go to build(), and you can find the appropriate path.

@NonNull public CsvFormatStrategy build() {
  if (date == null) {
    date = new Date();
  }
  if (dateFormat == null) {
    dateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss.SSS", Locale.UK);
  }
  if (logStrategy == null) {
    //The address is here.
    String diskPath = Environment.getExternalStorageDirectory().getAbsolutePath();
    String folder = diskPath + File.separatorChar + "logger";
    HandlerThread ht = new HandlerThread("AndroidFileLogger." + folder);
    ht.start();
    Handler handler = new DiskLogStrategy.WriteHandler(ht.getLooper(), folder, MAX_BYTES);
    logStrategy = new DiskLogStrategy(handler);
  }
  return new CsvFormatStrategy(this);
}

The specific path is: / storage/emulated/0/logger
Each file has a maximum of 500K, and the source code is pasted out.~~~~
private static final int MAX_BYTES = 500 * 1024; // 500K averages to a 4000 lines per file

The number after the generated file name logs_0.csv is incremented and the source code is pasted out.~~~~
newFile = new File(folder, String.format("%s_%s.csv", fileName, newFileCount));

markDown just doesn't work very well

I've finished my favorite part. Let's write down some routine operations. simple

I. Dependence on Logger

Address: https://github.com/orhanobut/logger
I didn't want to post the address. github is a good thing. I can't use github two days ago, so I'll stick it. Come on, I'm Lily's Tinker Bell.
The introduction on github is very detailed, but I want to paste it again.

rely on

dependencies {
    implementation 'com.orhanobut:logger:2.2.0'
}

Initialization

Logger.addLogAdapter(new AndroidLogAdapter());

Logger is already available here
Logger.d("debug");
Logger.e("error");
Logger.w("warning");
Logger.v("verbose");
Logger.i("information");
Logger.wtf("What a Terrible Failure");

Here's my own Logger usage

    val formatStrategy = PrettyFormatStrategy.newBuilder()
          .showThreadInfo(true)  //(optional) Whether to display thread information. The default value is true
          .methodCount(1)         // (optional) the number of method rows to display. Default 2
          .methodOffset(5)        // (Optional) Hide internal method calls to offsets. Default 5
          .tag("doShare")//(optional) Global markup for each log. Default PRETTY_LOGGER
          .build()
    Logger.addLogAdapter(AndroidLogAdapter(formatStrategy))//Set the appropriate adapter for logger according to the format above
    Logger.addLogAdapter(DiskLogAdapter())//Save to file

Topics: github Android