1. 程式人生 > >Logger日誌庫的基本使用

Logger日誌庫的基本使用

Logger

Logger是一款強大的開源日誌庫,可以實現清晰的列印效果

這裡寫圖片描述

引包

    compile 'com.orhanobut:logger:2.1.1'

初始化

        FormatStrategy formatStrategy = PrettyFormatStrategy.newBuilder()
                .showThreadInfo(true)  // 是否列印執行緒號,預設true
                .methodCount(5)         // 展示幾個方法數,預設2
                .methodOffset
(7) // (Optional) Hides internal method calls up to offset. Default 5 // .logStrategy(customLog) //是否更換列印輸出,預設為logcat .tag("meee") // 全域性的tag .build(); Logger.addLogAdapter(new AndroidLogAdapter(formatStrategy));

普通的列印

Logger.d("hello");
Logger.e
("hello"); Logger.w("hello"); Logger.v("hello"); Logger.wtf("hello");

支援列印陣列和集合(僅支援Debug模式)

        HashMap<String, String> map = new HashMap<>();
        map.put("key1","value1");
        map.put("key2","value2");
        map.put("key3","value3");
        ArrayList<String> list
= new ArrayList<>(); list.add("list1"); list.add("list2"); list.add("list3"); String[] strs={"strs1","strs2","strs3"}; Logger.d(list); Logger.d(map); Logger.d(strs);

一次性的tag

//只生效一次
Logger.t("tag_string"); 

支援xml和json

        JSONObject json = createJson();
        Logger.d(json.toString());
        Logger.json(json.toString());
        //xml同理
         Logger.xml("xmlString");

    //initData
    private JSONObject createJson() {
        try {
            JSONObject person = new JSONObject();
            person.put("phone", "12315");
            JSONObject address = new JSONObject();
            address.put("country", "china");
            address.put("province", "fujian");
            address.put("city", "xiamen");
            person.put("address", address);
            person.put("married", true);
            return person;
        } catch (JSONException e) {
            Logger.e(e, "create json error occured");
        }
        return null;
    }
//普通json列印
10-16 14:25:07.197 3743-3743/? D/meee-tag_string: │ {"phone":"12315","address":{"country":"china","province":"fujian","city":"xiamen"},"married":true}

//json列印效果
10-16 14:25:07.197 3743-3743/? D/meee: │ {
10-16 14:25:07.197 3743-3743/? D/meee: │   "phone": "12315",
10-16 14:25:07.197 3743-3743/? D/meee: │   "address": {
10-16 14:25:07.197 3743-3743/? D/meee: │     "country": "china",
10-16 14:25:07.197 3743-3743/? D/meee: │     "province": "fujian",
10-16 14:25:07.197 3743-3743/? D/meee: │     "city": "xiamen"
10-16 14:25:07.197 3743-3743/? D/meee: │   },
10-16 14:25:07.197 3743-3743/? D/meee: │   "married": true
10-16 14:25:07.197 3743-3743/? D/meee: │ }
10-16 14:25:07.197 3743-3743/? D/meee: └────────────────────────────────────────────────────────────────────────────────────────────────────────────────