13. Logger Android intensive course notes

1. Overview

When Android developers debug the program, they will more or less use log to check the status of the program. At the same time, the system will also prompt the R & D personnel through the log, information that the system needs to handle, such as exceptions, system warnings, etc. In summary, you can view the running status of the program through log (multithreading is particularly important).

2. log level

  1. Log.v: any message will be output. v here stands for verbose, which means verbose;
  2. Log.d: output only debug information;
  3. Log.i: general information;
  4. Log.w: for warning information, we usually need to pay attention to optimizing Android code;
  5. Log.e: error message.

Display level: V > d > I > W > e, that is, log filtering level.

The color of the log statement can be customized.

3,Logger

When it comes to Logger, it is generally simple, beautiful and powerful. Use it to print out the following information:

  • Thread information
  • Class information
  • Method information
  • Pretty print for JSON content
  • Clear output
  • Jump to source code Jump to source feature

1. Add logger dependency

implementation 'com.orhanobut:logger:2.2.0'

2.logger initialization

Logger.addLogAdapter(new AndroidLogAdapter());

3. Write MainActivity code as follows:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //Android Studio native log printing
        rawLog();

        //logger initialization
        Logger.addLogAdapter(new AndroidLogAdapter());

        //logger printing
        logger();
        //String format parameter
        stringFormat();
        //Supports collections (map, set, list, array, but only for debug logs)
        AMSL();
        //Support for json output (output at debug level)
        json();
    }

    private void rawLog() {
        Log.v("cxs", "current log");
        Log.d("cxs", "current log");
        Log.i("cxs", "current log");
        Log.w("cxs", "current log");
        Log.e("cxs", "current log");
    }

    private void logger() {
        Logger.d("debug");
        Logger.e("error");
        Logger.w("warning");
        Logger.v("verbose");
        Logger.i("information");
        Logger.wtf("What a Terrible Failure");
    }

    private void stringFormat() {
        Logger.d("hello %s", "world");
    }

    private void AMSL() {
        List<String> list = new LinkedList<>();
        list.add("aaa");
        list.add("bbb");
        list.add("ccc");
        Logger.d(list);

        Map<String, String> map = new HashMap<>();
        map.put("key1", "aaa");
        map.put("key2", "bbb");
        map.put("key3", "ccc");
        Logger.d(map);

        Set<String> set = new HashSet<>();
        set.add("aaa");
        set.add("bbb");
        set.add("ccc");
        Logger.d(set);

        Logger.d(new String[]{
                "aaa", "bbb", "ccc"
        });
    }

    private void json() {
        JSONObject jsonObject = new JSONObject();
        try {
            JSONObject person = new JSONObject();
            person.put("name", "chaixingsi");
            person.put("age", 22);
            jsonObject.put("person", person);

            JSONObject address = new JSONObject();
            address.put("province", "beijing");
            address.put("city", "beijing");
            jsonObject.put("address", address);
        } catch (JSONException e) {
            //ignore
        }

        Log.d("chaixingsi", "json:" + jsonObject);

        Logger.json(jsonObject.toString());
    }
}

4. Run each method from top to bottom, and the results are as follows.


  • Print results of Android Studio's own log:
  • logger print results:
  • Print result of string format parameter:
  • Set print results
  • Printing results of json content

For more usage of the Logger, see: https://github.com/orhanobut/logger

Tags: JSON Android less github

Posted on Wed, 08 Jan 2020 07:10:18 -0800 by nineseveninteractive