Android工具类——日志打印AppLogger

转载请注明出处 http://www.jianshu.com/p/f66bcb2a39d4

写Android时必不可少地会遇到日志打印的事情,这个工具类可以帮助我们简化log打印。这是我在看四次元微博客户端源码时找到的一个工具类,用起来非常好。这里能获取的一个知识点是,通过StackTraceElement获取当前打印日志的类名和方法名,这样就能自动获取我们平时需要手写的TAG信息,每一个类都写一个TAG还是挺麻烦的,能免则免。

/**
 * Wrapper API for sending log output.
 */
public class AppLogger {
    protected static final String TAG = "DSBBM";

    private AppLogger() {
    }

    /**
     * Send a VERBOSE log message.
     * 
     * @param msg
     *            The message you would like logged.
     */
    public static void v(String msg) {
            if (BuildConfig.DEBUG)
            android.util.Log.v(TAG, buildMessage(msg));
    }

    /**
     * Send a VERBOSE log message and log the exception.
     * 
     * @param msg
     *            The message you would like logged.
     * @param thr
     *            An exception to log
     */
    public static void v(String msg, Throwable thr) {
        if (BuildConfig.DEBUG)
            android.util.Log.v(TAG, buildMessage(msg), thr);
    }

    /**
     * Send a DEBUG log message.
     * 
     * @param msg
     */
    public static void d(String msg) {
        if (BuildConfig.DEBUG)
            android.util.Log.d(TAG, buildMessage(msg));
    }

    /**
     * Send a DEBUG log message and log the exception.
     * 
     * @param msg
     *            The message you would like logged.
     * @param thr
     *            An exception to log
     */
    public static void d(String msg, Throwable thr) {
        if (BuildConfig.DEBUG)
            android.util.Log.d(TAG, buildMessage(msg), thr);
    }

/**
 * Send an INFO log message.
 * 
 * @param msg
 *            The message you would like logged.
 */
public static void i(String msg) {
    if (BuildConfig.DEBUG)
        android.util.Log.i(TAG, buildMessage(msg));
}

    /**
     * Send a INFO log message and log the exception.
     * 
     * @param msg
     *            The message you would like logged.
     * @param thr
     *            An exception to log
     */
    public static void i(String msg, Throwable thr) {
        if (BuildConfig.DEBUG)
            android.util.Log.i(TAG, buildMessage(msg), thr);
    }

    /**
     * Send an ERROR log message.
     * 
     * @param msg
     *            The message you would like logged.
     */
    public static void e(String msg) {
        if (BuildConfig.DEBUG)
            android.util.Log.e(TAG, buildMessage(msg));
    }

    /**
     * Send a WARN log message
     * 
     * @param msg
     *            The message you would like logged.
     */
    public static void w(String msg) {
        if (BuildConfig.DEBUG)
            android.util.Log.w(TAG, buildMessage(msg));
    }

    /**
     * Send a WARN log message and log the exception.
     * 
     * @param msg
     *            The message you would like logged.
     * @param thr
     *            An exception to log
     */
    public static void w(String msg, Throwable thr) {
        if (BuildConfig.DEBUG)
            android.util.Log.w(TAG, buildMessage(msg), thr);
    }

    /**
     * Send an empty WARN log message and log the exception.
     * 
     * @param thr
     *            An exception to log
     */
    public static void w(Throwable thr) {
        if (BuildConfig.DEBUG)
            android.util.Log.w(TAG, buildMessage(""), thr);
    }

    /**
     * Send an ERROR log message and log the exception.
     * 
     * @param msg
     *            The message you would like logged.
     * @param thr
     *            An exception to log
     */
    public static void e(String msg, Throwable thr) {
        if (BuildConfig.DEBUG)
            android.util.Log.e(TAG, buildMessage(msg), thr);
    }

    /**
     * Building Message
     * 
     * @param msg
     *            The message you would like logged.
     * @return Message String
     */
    protected static String buildMessage(String msg) {
        StackTraceElement caller = new Throwable().fillInStackTrace().getStackTrace()[2];

        return new StringBuilder().append(caller.getClassName()).append(".").append(caller.getMethodName()).append("(): ").append(msg).toString();
    }
}

这个工具类主要在三处地方做了封装。
第一是加入BuildConfig.DEBUG判断,这样发布时就不会打印log,如果不了解BuildConfig,可以参考这篇文章Android BuildConfig.DEBUG的妙用
第二是log工具类都常见的,预设置TAG,这样不用每次都传两个参数
第三,通过StackTraceElement获取当前打印日志的类名和方法名,这个用来代替我们平时手写的TAG值。StackTrace用栈的形式保存了方法的调用信息。

这个工具类的关键方法是buildMessage

protected static String buildMessage(String msg) {
        StackTraceElement caller = new Throwable().fillInStackTrace().getStackTrace()[2];
        return new StringBuilder().append(caller.getClassName()).append(".").append(caller.getMethodName()).append("(): ").append(msg).toString();
    }
}

对StackTrace不了解的同学可以参考这篇文章StackTrace简述以及StackTraceElement使用实例

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,025评论 25 708
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,868评论 18 139
  • 文/程序员男神 前言 今早起的还算早,看了一场NBA,一想到工作还没给答复,心里就有点着急了。中午定了份外卖,心酸...
    程序猿男神阅读 1,625评论 2 12
  • 不知名的草儿,还参杂些去年的旧叶。咦,她在对我笑,我心里面即是舒服。 你或者已经毕业,或许在校,或许年过花甲,或许...
    一座城儿阅读 351评论 0 2