您当前的位置: 首页 >  android

Kevin-Dev

暂无认证

  • 0浏览

    0关注

    544博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

【Android -- 开源库】Logger 日志的基本使用

Kevin-Dev 发布时间:2022-03-30 08:30:00 ,浏览量:0

在这里插入图片描述

一、简介

Simple, pretty and powerful logger for android. 简单、漂亮、强大的安卓记录器。

GitHub 地址:https://github.com/orhanobut/logger

在 app/build.gradle 中添加如下依赖:

	// logger
    implementation 'com.orhanobut:logger:2.2.0'
功能
  • 线程的信息
  • 类的信息
  • 方法的信息
  • 格式打印 jsonxml
  • 点击链接跳转到源码打印处。
二、使用

1. 简单使用

		Logger.addLogAdapter(new AndroidLogAdapter());

        String name = "Kevin";
        Logger.i(name);

        Logger.clearLogAdapters();

打印结果: 在这里插入图片描述 可以看到上图打印的 TAG 是 PRETTY_LOGGER 。

2. 修改默认 TAG

		FormatStrategy formatStrategy = PrettyFormatStrategy.newBuilder()
                .showThreadInfo(false)  // (Optional) Whether to show thread info or not. Default true
                .methodCount(0)         // (Optional) How many method line to show. Default 2
                .methodOffset(3)        // (Optional) Skips some method invokes in stack trace. Default 5
//              .logStrategy(customLog) // (Optional) Changes the log strategy to print out. Default LogCat
                .tag("MainActivity")   // (Optional) Custom tag for each log. Default PRETTY_LOGGER
                .build();

        Logger.addLogAdapter(new AndroidLogAdapter(formatStrategy));
        Logger.addLogAdapter(new AndroidLogAdapter() {
            @Override
            public boolean isLoggable(int priority, String tag) {
                return BuildConfig.DEBUG;
            }
        });
        Logger.addLogAdapter(new DiskLogAdapter());

        String name = "Kevin";
        Logger.i(name);

        Logger.clearLogAdapters();

打印结果: 在这里插入图片描述

3. 拼接成字符串

		FormatStrategy formatStrategy = PrettyFormatStrategy.newBuilder()
                .showThreadInfo(false)  // (Optional) Whether to show thread info or not. Default true
                .methodCount(0)         // (Optional) How many method line to show. Default 2
                .methodOffset(3)        // (Optional) Skips some method invokes in stack trace. Default 5
//              .logStrategy(customLog) // (Optional) Changes the log strategy to print out. Default LogCat
                .tag("MainActivity")   // (Optional) Custom tag for each log. Default PRETTY_LOGGER
                .build();

        Logger.addLogAdapter(new AndroidLogAdapter(formatStrategy));
        Logger.addLogAdapter(new AndroidLogAdapter() {
            @Override
            public boolean isLoggable(int priority, String tag) {
                return BuildConfig.DEBUG;
            }
        });
        Logger.addLogAdapter(new DiskLogAdapter());

        String name = "Kevin";
        int age = 20;
        Logger.i("大家好,我叫%s,今年%d,很高兴大家来看我的文章!!!", name, age);

        Logger.clearLogAdapters();

打印结果: 在这里插入图片描述

4. 打印 json 数据

	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_logger);

        FormatStrategy formatStrategy = PrettyFormatStrategy.newBuilder()
                .showThreadInfo(false)
                .methodCount(0)
                .tag("MainActivity")
                .build();

        Logger.addLogAdapter(new AndroidLogAdapter(formatStrategy));
        Logger.i("no thread info and method info");

        Logger.t("tag").e("Custom tag for only one use");

        String json = createJson().toString();
        Logger.json(json);

        Logger.clearLogAdapters();
    }

    // 创建json数据
    private JSONObject createJson() {
        try {
            JSONObject person = new JSONObject();
            person.put("phone", "13888888888");
            JSONObject address = new JSONObject();
            address.put("country", "China");
            address.put("province", "GuangDong");
            address.put("city", "ShenZhen");
            person.put("address", address);
            person.put("married", true);
            return person;
        } catch (JSONException e) {
            Logger.e(e, "create json error occured");
        }
        return null;
    }

打印结果: 在这里插入图片描述

5. 打印数组、List、Map 等对象数据

		FormatStrategy formatStrategy = PrettyFormatStrategy.newBuilder()
                .showThreadInfo(false)
                .methodCount(0)
                .tag("MainActivity")
                .build();

        Logger.addLogAdapter(new AndroidLogAdapter(formatStrategy));

        String[] names = {"Jerry", "Emily", "小五", "hongyang", "七猫"};
        Logger.d(names);

        List users = new ArrayList();
        for (int i = 0; i             
关注
打赏
1658837700
查看更多评论
0.0404s