Android/Java日志打印工具类

来源:互联网 发布:淘宝子账号怎么添加 编辑:程序博客网 时间:2024/06/12 22:07

1. 代码

/** * log日志打印工具 * Created by Jero on 2016/11/28. */public class LogUtils {    private String message = null;    private String tag = null;    public static int LEVEL_I = 0;    public static int LEVEL_D = 1;    public static int LEVEL_W = 2;    public static int LEVEL_E = 3;    private int levelCode = LEVEL_I;    /**     * 设置TAG和日志等级     *     * @param tag       设置TAG标签,方便过滤。为空则默认使用LogUtils的类名作为打印的TAG。     * @param levelCode 要打印的日志等级码,{@link #LEVEL_I}、{@link #LEVEL_D}、{@link #LEVEL_W}、{@link #LEVEL_E}     * @return     */    public static LogUtils init(String tag, int levelCode) {        LogUtils instance = new LogUtils();        // 初始化tag        if (TextUtils.isEmpty(tag))            tag = LogUtils.class.getName();        instance.tag = tag;        // 初始化message        if (instance.message == null)            instance.message = "";        // 初始化等级        if (levelCode < LEVEL_I)            levelCode = LEVEL_I;        else if (levelCode > LEVEL_E)            levelCode = LEVEL_E;        instance.levelCode = levelCode;        return instance;    }    /**     * 添加一个key : value形式的log     * @param key 键     * @param value 值     * @return     */    public LogUtils append(String key, String value) {        message += " " + key + " : " + value + " ,";        return this;    }    /**     * 添加一个仅有值的log     * @param value 值     * @return     */    public LogUtils append(String value) {        message += " " + value + " ,";        return this;    }    /**     * log打印终止方法     */    public void print() {        message = message.substring(0, message.lastIndexOf(",")) + "。";        Log.e(tag, message);    }}

2. 使用方法

  1. 调用init方法设置一个TAG,如同Log.e(TAG,”“);中的TAG。
  2. 直接调用append方法,可以设置key-value打印,也可以直接打印value。
  3. 调用print方法结束此次打印。

例:

LogUtils.init(TAG, LogUtils.LEVEL_E)         .append("打印等级为ERROR的一条日志")         .append("key", "value")         .print();

这里写图片描述

3. 打印示例

12-22 13:45:58.890 6524-6524/? E/cn.ijero.view.activity.SplashActivity:  正在查询外部存储卡的可操作信息 , extSdcard Path : /mnt/ext_sdcard , canExecute : true , canRead : true , canWrite : true , 上面为外部存储卡的可操作信息 。
0 0
原创粉丝点击