Logger logging framework using tutorial and source code analysis

Logger is introduced

In daily development, Log is an essential step, data view, bug tracking, data collection, etc., all need to use Log, Android system provides the Log class to achieve, but each time to enter a TAG, will be very troublesome. So you can either package your own Log framework, or use open source logging frameworks such as Timer, XLog, Logger, etc. I’ll focus on Logger here.

github.com/orhanobut

Simple, pretty and powerful Logger for Android is made by Orhan Obut.

Logger supports several types:

String, String format, arrays, collections, XML and JSON, but also can store logs to files, also can output thread methods and other details

The Logger using

Gradle dependencies:

Implementation 'com. Orhanobut: logger: 2.2.0'Copy the code

2. Add the Adapter

Logger.addLogAdapter(new AndroidLogAdapter());
Copy the code

The next step is to use it directly

Logger.d("message");
Copy the code

Default output Log and the class name, method name, line number, thread and other related information. If you don’t want to display information other than Log, you can set the Adapter accordingly.

FormatStrategy FormatStrategy = PrettyFormatStrategy. NewBuilder (.) showThreadInfo (false)/show/thread information methodCount (5) / / MethodOffset (0) // Skip stack method number, default is 0. Tag ("My custom tag") // tag content The default is pretty_logger.build (); Logger.addLogAdapter(new AndroidLogAdapter(formatStrategy));Copy the code

If methodOffset is 1, mainactivity.oncreate () is not displayed and only the other four are displayed.

If you need to log is stored into a local file, you need to use DiskLogAdapter, also need to declare to store permissions, Logger default generated CSV file, stored in the/storage/emulated / 0 / Logger directory,

Logger.addLogAdapter(new DiskLogAdapter());
Copy the code

If Logger configuration is different for different pages, use Logger.ClearLogadapters () and then reconfigure.

3. Other types

A message can be a collection, an array,

Logger.t("tag").e("Custom tag for only one use"); Logger.json("{ \"key\": 3, \"value\": something}"); Logger.d(Arrays.asList("foo", "bar")); Logger. D (new int [],3,19,4 {2}); Logger.d(""); Map<String, String> map = new HashMap<>(); map.put("key", "value"); map.put("key1", "value2"); Logger.d(map); Logger.d("message %s", " erro");Copy the code

Logger source code analysis

1. Call sequence

Logger finally calls log.println (int priority, String tag, String MSG). Take Logger.d() as an example

Logger.d(“message”);

Point to the loggerprinter.d () method, point to log(), and since you can add more than one adapter at a time, do a for loop here that iterates through all of them

Take AndroidLogAdapter as an example, point to the log() method of AndroidLogAdapter, and finally use the system log.println () to output the log.

2. Storage analysis

Uploading client logs is a crucial step for analyzing app operation and users’ usage habits. However, the Logger storage path is fixed and no relevant API is provided for setting, so we can customize an Adapter by analyzing its principle. For the path here, I suggest storing it in /Android/data/ package name, because this path does not require user permission and can be used directly.

With the new DiskLogAdapter() constructor, CsvFormatStrategy creates an instance of CsvFormatStrategy using Builder mode. In Builder (), you can see the path. Because once I’m in a child thread, Handler need to manually start the stars, so here are obtained through HandlerThread which is passed to the Handler, through DiskLogStrategy. WriteHandler create Handler, Will pass the file path to DiskLogStrategy WriteHandler, by constructing method to generate DiskLogStrategy object at the same time, The handler is passed to DiskLogStrategy. When a Log is output, DiskLogStrategy’s Log () method is called. The message is sent by the handler in the Log () method. And then to the handler processing, storage to DiskLogStrategy. The path of WriteHandler.

First time to Denver blog, welcome correction

end