zoukankan      html  css  js  c++  java
  • 一个好用的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/

  • 相关阅读:
    文章索引
    Rancher pipeline 实现简单跟踪
    rancher 应用商店
    nginx ingress 在aks 上安装
    go countdown
    go channel pipeline 套路
    gorm使用
    华为云cce pvc 指定云硬盘云存储
    influxdb 基本概念
    Python3处理xlsx去掉含有特定字符的行
  • 原文地址:https://www.cnblogs.com/yaxiaoke/p/4860358.html
Copyright © 2011-2022 走看看