zoukankan      html  css  js  c++  java
  • Android开发日志统一管理

    在开发中,我们通常要对日志的输出做统一管理,下面就为大家推荐一个日志输出类,在开发阶段只需将DEBUG常量设为true,生产环境将DEBUG设为false即可控制日志的输出。啥都不说了,需要的朋友直接带走。

    package com.android.util;
    
    /**
     * 日志输出统一管理
     * 
     * @author 祁连山
     * @date 2015-04-27
     * @version 1.0
     * 
     */
    public class Log {
    
        private static final String TAG = "com.android.app";
        private static final boolean DEBUG = true;
    
        private static String getFunctionName() {
            StackTraceElement[] sts = Thread.currentThread().getStackTrace();
            if (sts == null) {
                return null;
            }
            for (StackTraceElement st : sts) {
                if (st.isNativeMethod()) {
                    continue;
                }
                if (st.getClassName().equals(Thread.class.getName())) {
                    continue;
                }
                if (st.getClassName().equals(Log.class.getName())) {
                    continue;
                }
                return "[" + Thread.currentThread().getName() + "(" + Thread.currentThread().getId() + "): " + st.getFileName() + ":" + st.getLineNumber() + "]";
            }
            return null;
        }
    
        private static String createMessage(String msg) {
            String functionName = getFunctionName();
            String message = (functionName == null ? msg : (functionName + " - " + msg));
            return message;
        }
    
        public static void i(String msg) {
            if (DEBUG) {
                String message = createMessage(msg);
                android.util.Log.i(TAG, message);
            }
        }
    
        public static void v(String msg) {
            if (DEBUG) {
                String message = createMessage(msg);
                android.util.Log.v(TAG, message);
            }
        }
    
        public static void d(String msg) {
            if (DEBUG) {
                String message = createMessage(msg);
                android.util.Log.d(TAG, message);
            }
        }
    
        public static void e(String msg) {
            if (DEBUG) {
                String message = createMessage(msg);
                android.util.Log.e(TAG, message);
            }
        }
    
        public static void w(String msg) {
            if (DEBUG) {
                String message = createMessage(msg);
                android.util.Log.w(TAG, message);
            }
        }
    
        public static void e(Exception e) {
            if (DEBUG) {
                StringBuffer sb = new StringBuffer();
                String name = getFunctionName();
                StackTraceElement[] sts = e.getStackTrace();
    
                if (name != null) {
                    sb.append(name + " - " + e + "
    ");
                } else {
                    sb.append(e + "
    ");
                }
                if (sts != null && sts.length > 0) {
                    for (StackTraceElement st : sts) {
                        if (st != null) {
                            sb.append("[ " + st.getFileName() + ":" + st.getLineNumber() + " ]
    ");
                        }
                    }
                }
                android.util.Log.e(TAG, sb.toString());
            }
        }
    }
  • 相关阅读:
    高通电池曲线(转)
    随笔分类
    海思uboot启动流程详细分析(转)
    GPRS以TCP上传数据到服务器OK,但收不到服务器下发的数据
    WIS800C TCP Client 非透传模式连接远程服务器使用方法(转)
    MySQL 不允许从远程访问的解决方法
    Linux下将Mysql和Apache加入到系统服务里的方法
    linux服务器出现严重故障后的原因以及解决方法
    Linux防火墙该如何设置
    Linux下的两个聊天命令的使用方法
  • 原文地址:https://www.cnblogs.com/magics/p/4484803.html
Copyright © 2011-2022 走看看