Android Log工具类

一、Log的基本格式

如下为Android Studio抓取的一条Log:

格式:
date time PID-TID/package priority/tag: message

例子:
2022-09-21 19:34:15.293 14923-14923/com.lzq.mycustomdemo D/linzhiqin: LZQ IS GOOD BOY!!!

格式定义如下:

格式描述
date日志输出时设备的日期
time日志输出时设备的时间,精确到毫秒
PID进程标识符
TID线程标识符,若进程中只存在一个线程,则PID与TID可以相同
package输出该条日志的应用包名
priority日志优先级,取值如下,优先级从低到高排列(V:详细 D:调试 I:信息 W:警告 E:错误 A:断言)
tag标签,用来标明日志发起者以及方便对日志进行过滤筛选
message日志的主体内容

二、为什么要自定义Log类

  1. 方便应用全局管理Log的输出;
  2. 更友好得展示应用的log,有助于开发者调试、定位问题;

三、Log工具类实现

package com.lzq.mycustomdemo.utils;

import android.util.Log;

/**
 * @author: linzhiqin
 * @date: 2022/9/21
 */
public class LogUtil {
    public static String tag = "linzhiqin";

    public static int VERBOSE = 1;
    public static int DEBUG = 2;
    public static int INFO = 3;
    public static int WARN = 4;
    public static int ERROR = 5;
    public static int NOTHING = 6;

    public static int LEVEL = VERBOSE;//当前Log的打印等级
    private static boolean needDetail = false;//是否需要Log详细信息

    public static void v(String msg) {
        if (LEVEL <= VERBOSE) {
            Log.v(tag, getDetailInfo() + msg);
        }
    }

    public static void d(String msg) {
        if (LEVEL <= DEBUG) {
            Log.d(tag, getDetailInfo() + msg);
        }
    }

    public static void i(String msg) {
        if (LEVEL <= INFO) {
            Log.i(tag, getDetailInfo() + msg);
        }
    }

    public static void w(String msg) {
        if (LEVEL <= WARN) {
            Log.w(tag, getDetailInfo() + msg);
        }
    }

    public static void e(String msg) {
        if (LEVEL <= ERROR) {
            Log.e(tag, getDetailInfo() + msg);
        }
    }

    private static String getDetailInfo() {
        if (needDetail) {
            return getClassName() + "$" + getMethodName() + "(" + getLineNumber() + ")"+ ": ";
        }
        return "";
    }

    /**
     * 获取Log所在的类名
     * @return
     */
    private static String getClassName() {
        try {
            String classPath = Thread.currentThread().getStackTrace()[5].getClassName();
            return classPath.substring(classPath.lastIndexOf(".") + 1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取Log所在的方法名
     * @return
     */
    private static String getMethodName() {
        try {
            return Thread.currentThread().getStackTrace()[5].getMethodName();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取Log所在的行
     * @return
     */
    private static int getLineNumber() {
        try {
            return Thread.currentThread().getStackTrace()[5].getLineNumber();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }
}

四、代码仓库地址

Demo地址:  https://gitee.com/linzhiqin/custom-demo

  • 14
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 13
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值