LogUtil 工具类

LogUtil 工具类

public final class LogUtil {

    //all、verbose、debug、info、warn、err Log print on-off
    private final static boolean all = true;
    private final static boolean i = true;
    private final static boolean d = true;
    private final static boolean e = true;
    private final static boolean v = true;
    private final static boolean w = true;
    //default print tag
    private final static String defaultTag = "TAG";

    public static void V(String msg) {
        if (all && v) {
            android.util.Log.v(defaultTag, msg);
        }
    }

    public static void V(String tag, String msg) {
        if (all && v) {
            android.util.Log.v(tag, msg);
        }
    }

    public static void D(String msg) {
        if (all && d) {
            android.util.Log.d(defaultTag, msg);
        }
    }

    public static void D(String tag, String msg) {
        if (all && d) {
            android.util.Log.d(tag, msg);
        }
    }

    public static void I(String msg) {
        if (all && i) {
            android.util.Log.i(defaultTag, msg);
        }
    }

    public static void I(String tag, String msg) {
        if (all && i) {
            android.util.Log.i(tag, msg);
        }
    }

    public static void W(String msg) {
        if (all && w) {
            android.util.Log.w(defaultTag, msg);
        }
    }

    public static void W(String tag, String msg) {
        if (all && w) {
            android.util.Log.w(tag, msg);
        }
    }

    public static void E(String msg) {
        if (all && e) {
            android.util.Log.e(defaultTag, msg);
        }
    }

    public static void E(String tag, String msg) {
        if (all && e) {
            android.util.Log.e(tag, msg);
        }
    }

    //log长度大于4K 限制
    public static void longI(String tag, String msg) {
        if (all && i) {
            int lenLimit = 4000, endPos;
            if (null != msg && msg.length() < lenLimit) {
                I(msg);
                return;
            }

            for (int i = 0, len = msg.length(); i < len; i += lenLimit) {
                endPos = (i + lenLimit) < len ? (i + lenLimit) : len;
                I(msg.substring(i, endPos));
            }
        }
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Android开发中,我们经常会使用Log来打印日志。但是,Android原生的Log输出信息有限,为了更好地输出和管理日志,我们可以自定义LogUtil。 下面是一个简单的LogUtil类,可以自定义输出日志级别、输出信息格式等: ```java public class LogUtil { private static final String TAG = "LogUtil"; private static boolean sDebug = true; // 是否打印日志 private static boolean sSaveLog = false; // 是否保存日志到文件 private static String sLogPath = Environment.getExternalStorageDirectory().getPath() + "/log.txt"; // 日志保存路径 public static void setDebug(boolean debug) { sDebug = debug; } public static void setSaveLog(boolean saveLog) { sSaveLog = saveLog; } public static void setLogPath(String logPath) { sLogPath = logPath; } public static void v(String msg) { if (sDebug) { Log.v(TAG, msg); } saveLogToFile("V", TAG, msg); } public static void d(String msg) { if (sDebug) { Log.d(TAG, msg); } saveLogToFile("D", TAG, msg); } public static void i(String msg) { if (sDebug) { Log.i(TAG, msg); } saveLogToFile("I", TAG, msg); } public static void w(String msg) { if (sDebug) { Log.w(TAG, msg); } saveLogToFile("W", TAG, msg); } public static void e(String msg) { if (sDebug) { Log.e(TAG, msg); } saveLogToFile("E", TAG, msg); } private static void saveLogToFile(String level, String tag, String msg) { if (sSaveLog) { try { File file = new File(sLogPath); FileWriter fw = new FileWriter(file, true); BufferedWriter bw = new BufferedWriter(fw); bw.write("[" + level + "] " + tag + ": " + msg + "\n"); bw.close(); fw.close(); } catch (IOException e) { Log.e(TAG, "saveLogToFile: " + e.getMessage()); } } } } ``` 使用方法: ```java LogUtil.setDebug(true); // 设置是否打印日志 LogUtil.setSaveLog(true); // 设置是否保存日志到文件 LogUtil.setLogPath("/sdcard/log.txt"); // 设置日志保存路径 LogUtil.v("verbose log"); LogUtil.d("debug log"); LogUtil.i("info log"); LogUtil.w("warning log"); LogUtil.e("error log"); ``` 以上就是一个简单的LogUtil类的实现,可以根据自己的需求进行修改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值