android 自定义log打印

自定义log打印

自定义一个log打印类(单例模式),使用属性控制log打印开关和log的级别,使用的时候要注意,一定要在使用完后,如在activity的onDestory中调用release()函数释放静态类。代码如下:

/************************************************* 
 * @author SDT13001 
 * @E-mail  
 * @date 2016年12月16日 上午11:26:55 
 * @version 1.0 
*************************************************/


import android.os.SystemProperties;
import android.util.Log;

public class SkLog {
    private static String tag = "AutoTest";
    public static int logLevel = Log.VERBOSE;
    public static boolean isDebug = true;
    private static SkLog mLog = null;

    /*
     * the property that control log open or closed:
     * Values 'n', 'no', '0', 'false' or 'off' are considered false.Values 'y', 'yes', '1', 'true' or 'on' are considered true.
     */
    private static String PRO_LOG = "persist.sys.factory_log";

    //thr property control log level ,default is 2
    private static String PRO_LOG_LEVEL = "persist.sys.factory_log_level";

    public  SkLog(){
        isDebug = SystemProperties.getBoolean(PRO_LOG,isDebug);
        logLevel = SystemProperties.getInt(PRO_LOG_LEVEL, logLevel);
    }

    public synchronized static void release(){
        mLog = null;
    }

    private  String getFunctionName() {
        StackTraceElement[] stacktrace = Thread.currentThread().getStackTrace();

        if (stacktrace == null) {
            return null;
        }
        /*for (int i = 0; i < stacktrace.length; i++) {
            System.out.println("----  the " + i + " element  ----");
            System.out.println("isNativeMethod: "+ stacktrace[i].isNativeMethod() );
            System.out.println("toString: " + stacktrace[i].toString());
            System.out.println("ClassName: " + stacktrace[i].getClassName());
            System.out.println("FileName: " + stacktrace[i].getFileName());
            System.out.println("LineNumber: " + stacktrace[i].getLineNumber());
            System.out.println("MethodName: " + stacktrace[i].getMethodName());
        }*/
        for(StackTraceElement st:stacktrace) {
            if(st.isNativeMethod()) {
                continue;
            }

            if(st.getClassName().equals(Thread.class.getName())) {
                continue;
            }

            if(st.getClassName().equals(this.getClass().getName())) {
                continue;
            }

            return st.getFileName()+"(line:"+st.getLineNumber()+")";
        }

        return null;
    }

    private static void info(Object str) {
        if(null == mLog){
            mLog = new SkLog();
        }
        if (logLevel <= Log.INFO) {         
            String name = mLog.getFunctionName();
            String ls=(name==null?str.toString():(name+" - "+str));
            Log.i(tag, ls);
        }
    }

    public static void i(Object str) {
        if(null == mLog){
            mLog = new SkLog();
        }
        if (isDebug) {
            info(str);
        }
    }

    private static void verbose(Object str) {
        if(null == mLog){
            mLog = new SkLog();
        }
        if(logLevel <= Log.VERBOSE) {            
            String name = mLog.getFunctionName();
            String ls=(name==null?str.toString():(name+" - "+str));
            Log.v(tag, ls);    
        }
    }

    public static void v(Object str) {
        if(null == mLog){
            mLog = new SkLog();
        }
        if (isDebug) {
            verbose(str);
        }
    }

    private static void warn(Object str) {
        if(null == mLog){
            mLog = new SkLog();
        }
        if (logLevel <= Log.WARN) {         
            String name = mLog.getFunctionName();
            String ls=(name==null?str.toString():(name+" - "+str));
            Log.w(tag, ls);
        }
    }

    public static void w(Object str) {
        if(null == mLog){
            mLog = new SkLog();
        }
        if (isDebug) {
            warn(str);
        }
    }

    private static void error(Object str) {
        if(null == mLog){
            mLog = new SkLog();
        }
        if(logLevel <= Log.ERROR) {            
            String name = mLog.getFunctionName();
            String ls=(name==null?str.toString():(name+" - "+str));
            Log.e(tag, ls);
        }
    }

    public static void e(Object str) {
         if(null == mLog){
            mLog = new SkLog();
         }

         if (isDebug) {
             error(str);
         }
    }

    private static void error(Exception ex) {
        if(null == mLog){
            mLog = new SkLog();
        }
        if(logLevel <= Log.ERROR) {
            StringBuffer sb = new StringBuffer();
            String name = mLog.getFunctionName();

            StackTraceElement[] sts = ex.getStackTrace();

            if (name != null) {
                sb.append(name+" - "+ex+"\r\n");
            } else {
                sb.append(ex+"\r\n");
            }

            if(sts != null && sts.length > 0) {
                for(StackTraceElement st:sts) {
                    if(st != null) {
                        sb.append("[ "+st.getFileName()+":"+st.getLineNumber()+" ]\r\n");
                    }
                }
            }

            Log.e(tag, sb.toString());
        }
    }

    public static void e(Exception ex) {
        if(null == mLog){
            mLog = new SkLog();
        }
        if (isDebug) {
            error(ex);
        }
    }

    private static void debug(Object str) {
        if(null == mLog){
            mLog = new SkLog();
        }
        if(logLevel <= Log.DEBUG) {
            String name = mLog.getFunctionName();
            String ls=(name==null?str.toString():(name+" - "+str));
            Log.d(tag, ls);
        }
    }

    public static void d(Object str) {
        if(null == mLog){
            mLog = new SkLog();
        }
        if (isDebug) {
            debug(str);
        }
    }
}

你可能感兴趣的:(android,app,java)