第三方开源库-->Logger 日志打印

Posted Kevin_小飞象

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第三方开源库-->Logger 日志打印相关的知识,希望对你有一定的参考价值。

简介

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<String> users = new ArrayList<>();
        for (int i = 0; i < names.length; i++) 
            users.add("kevin" + i);
        
        Logger.t("tag").d(users);


        Map<String, String> map = new HashMap<>();
        map.put("key", "value");
        map.put("key1", "value2");
        Logger.d(map);

        Logger.clearLogAdapters();

打印结果:

以上是关于第三方开源库-->Logger 日志打印的主要内容,如果未能解决你的问题,请参考以下文章

Android 开源日志库 Logger 使用教程

爆料喽!!!开源日志库Logger的使用秘籍

爆料喽!!!开源日志库Logger的使用秘籍

Android开源日志库Logger

第三方日志库logrus使用

第三方日志库logrus使用