美文网首页Android开发技巧
Android工具类——日志打印AppLogger

Android工具类——日志打印AppLogger

作者: 黑岛様 | 来源:发表于2015-04-08 14:41 被阅读2683次

    转载请注明出处 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工具类——日志打印AppLogger

      本文链接:https://www.haomeiwen.com/subject/fbbzxttx.html