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/

  • 相关阅读:
    SCOM 初探 [SCOM应用系列之一]
    SCOM 安装部署 [SCOM应用系列之二]
    CMMI 配置管理(Configuration Management)系列(1) 简介
    设计模式总结之创建型设计模式
    tabbar图片渲染的问题
    react实现自定义hooks(节流和防抖)
    前端工程化5js源码编译和ast
    react实现自定义hooks(跑马灯)
    react实现自定义hooks(倒计时)
    react实现自定义hooks(移动端拖拽)
  • 原文地址:https://www.cnblogs.com/yaxiaoke/p/4860358.html
Copyright © 2011-2022 走看看