zoukankan      html  css  js  c++  java
  • 使用android日志工具

    Log的级别?

    日志级别按照高低排序为:ERROR,WARN,INFO,DEBUG,VERBOSE,

    日志输出:

    Log.e()输出ERROR级别的日志信息

    Log.w()输出WARN,ERROR级别的日志

    Log.i()输出WARN,ERROR级别的日志

    Log.d() 输出DEBUG,INFO,WARN,ERROR级别的日志

    Log.v () 输出VERBOSE,DEBUG,INFO,WARN,ERROR级别的日志

    Log的封装?

    平时在开发的过程中,总是随手就来Log.e(),Log.w(),Log,i().........调试的不亦乐乎,等项目完成准备上线时,这些日志咋办呢?留着?显得不够正式,删了?费时费力伤神,以后维护指不定这些日志信息能帮忙保住饭碗,那咋整?LogUtil,开发的时候打印,发布时不打印。

    package com.kbr.issuestandard.utils;
    
    import android.util.Log;
    
    /**
     * LOG工具类
     */
    public class LogUtil {
        private final static boolean isDebug = true;
        private final static String TAG = "LogUtil";
    
        public static void v(String tag, String msg) {
            if (isDebug)
                Log.v(tag, msg);
        }
    
        public static void v(String msg) {
            if (isDebug)
                Log.v(TAG, msg);
        }
    
        public static void d(String tag, String msg) {
            if (isDebug)
                Log.d(tag, msg);
        }
    
        public static void d(String msg) {
            if (isDebug)
                Log.d(TAG, msg);
        }
    
        public static void i(String tag, String msg) {
            if (isDebug)
                Log.i(tag, msg);
        }
    
        public static void i(String msg) {
            if (isDebug)
                Log.i(TAG, msg);
    
        }
    
        public static void w(String tag, String msg) {
            if (isDebug)
                Log.w(tag, msg);
        }
    
        public static void w(String msg) {
            if (isDebug)
                Log.w(TAG, msg);
        }
    
        public static void e(String tag, String msg) {
            if (isDebug)
                Log.e(tag, msg);
        }
    
        public static void e(String msg) {
            if (isDebug)
                Log.e(TAG, msg);
        }
    }

    使用强大的Logger

    考虑到系统的log硬件条件太基础啦,我们不妨使用简单,强大的Logger框架,项目地址为 https://github.com/orhanobut/logger

    1:首先我们先在项目中添加依赖(implementation 'com.orhanobut:logger:2.2.0')

        //logger
        implementation "com.orhanobut:logger:$rootProject.loggerVersion"

    2:在Application中配置初始化(非必须,有默认的)

    private void initLogger() {
    FormatStrategy formatStrategy = PrettyFormatStrategy.newBuilder()
    .showThreadInfo(false) // 是否显示线程,默认显示
    .methodCount(3) // 显示多少方法 默认 2
    .methodOffset(7) // 设置方法的偏移量. 默认是 5
    .tag("My custom tag")
    .build();
    Logger.addLogAdapter(new AndroidLogAdapter(formatStrategy));
    Logger.addLogAdapter(new AndroidLogAdapter(){
    @Override
    public boolean isLoggable(int priority, @Nullable String tag) {
    //返回true,打印日志,返回false ,不打印日志,可调试时返回true,发布时返回false
    return BuildConfig.DEBUG;
    }
    });
    //保存日志到文件中
    Logger.addLogAdapter(new DiskLogAdapter());
    }

    3:使用方法

    打印不同级别日志

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

    支持字符串格式参数

    Logger.d("hello %s", "world");

    支持集合输出(仅适用于Debug 模式)

    Logger.d(MAP);
    Logger.d(SET);
    Logger.d(LIST);
    Logger.d(ARRAY);

    支持Json和Xml格式数据输出(仅适用于Debug 模式)

    Logger.json(JSON_CONTENT);
    Logger.xml(XML_CONTENT);

    将日志保存到文件

    Logger.addLogAdapter(new DiskLogAdapter());
  • 相关阅读:
    PyQt作品 – PingTester – 多点Ping测试工具
    关于和技术人员交流的一二三
    Pyjamas Python Javascript Compiler, Desktop Widget Set and RIA Web Framework
    Hybrid Qt applications with PySide and Django
    pyjamas build AJAX apps in Python (like Google did for Java)
    PyQt 维基百科,自由的百科全书
    InfoQ:请问为什么仍要选择Java来处理后端的工作?
    Eric+PyQt打造完美的Python集成开发环境
    python select module select method introduce
    GUI Programming with Python: QT Edition
  • 原文地址:https://www.cnblogs.com/bdsdkrb/p/10038732.html
Copyright © 2011-2022 走看看