• 一个好用的Log管理类


    public class L {
        private static String className;            //所在的类名
        private static String methodName;            //所在的方法名
        private static int lineNumber;                //所在行号
        
        public static final int VERBOSE = 1;          //显示Verbose及以上的Log
        public static final int DEBUG = 2;            //显示Debug及以上的Log
        public static final int INFO = 3;            //显示Info及以上的Log
        public static final int WARN = 4;            //显示Warn及以上的Log
        public static final int ERROR = 5;            //显示Error及以上的Log
        public static final int NOTHING = 6;        //全部不显示
        
        public static final int LEVEL = NOTHING;    //控制显示的级别
    
        private L() {
        }
    
        public static boolean isDebuggable() {
            return BuildConfig.DEBUG;
        }
    
        private static String createLog(String log) {
    
            StringBuffer buffer = new StringBuffer();
            buffer.append("[");
            buffer.append(methodName);
            buffer.append(":");
            buffer.append(lineNumber);
            buffer.append("]");
            buffer.append(log);
    
            return buffer.toString();
        }
    
        private static void getMethodNames(StackTraceElement[] sElements) {
            className = sElements[1].getFileName();
            methodName = sElements[1].getMethodName();
            lineNumber = sElements[1].getLineNumber();
        }
        
        public static void v(String message) {
            if (!isDebuggable()) {
                return;
            }
    
            if (LEVEL <= VERBOSE) {
                getMethodNames(new Throwable().getStackTrace());
                Log.v(className, createLog(message));
            }
        }
    
        public static void d(String message) {
            if (!isDebuggable()) {
                return;
            }
    
            if (LEVEL <= DEBUG) {
                getMethodNames(new Throwable().getStackTrace());
                Log.d(className, createLog(message));
            }
        }
    
        public static void i(String message) {
            if (!isDebuggable()) {
                return;
            }
    
            if (LEVEL <= INFO) {
                getMethodNames(new Throwable().getStackTrace());
                Log.i(className, createLog(message));
            }
        }
    
        public static void w(String message) {
            if (!isDebuggable()) {
                return;
            }
    
            if (LEVEL <= WARN) {
                getMethodNames(new Throwable().getStackTrace());
                Log.w(className, createLog(message));
            }
        }
        
        public static void e(String message) {
            if (!isDebuggable()) {
                return;
            }
    
            if (LEVEL <= ERROR) {
                getMethodNames(new Throwable().getStackTrace());
                Log.e(className, createLog(message));
            }
        }
    }

    原文地址:http://www.devwiki.net/2015/06/24/Android-New-Log/

  • 相关阅读:
    我的学习之路_第八章_map集合
    我的学习之路_第七章_list集合,set集合
    我的学习之路_第六章_迭代器,泛型
    我的学习之路_第五章_Data,正则
    我的学习之路_第四章_异常
    我的学习之路_第三章_匿名内部类
    我的学习之路_第二章_接口/多态
    如何使用电脑上的谷歌浏览器来调试安卓手机上的移动端页面
    跨域之jsonp
    跨域之同源策略 Same-origin policy
  • 原文地址:https://www.cnblogs.com/yaxiaoke/p/4860358.html
Copyright © 2020-2023  润新知