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: └────────────────────────────────────────────────────────────────────────────────────────────────────────────────
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值