• 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());
            }
        }
    }
  • 相关阅读:
    第15届创新英语大赛初赛第二阶段题目
    13.递归第一次
    13.递归第一次
    13.递归第一次
    13.递归第一次
    Algs4-1.3.19给出一段代码,删除链表的尾结点
    Algs4-1.3.18下面链表在代码的作用
    Algs4-1.3.17从文件中读取信息生成Transaction对象数组
    Algs4-1.3.16从文件中读取日期生成日期对象数组
    Algs4-1.3.15打印Queue中倒数第k个元素的值
  • 原文地址:https://www.cnblogs.com/magics/p/4484803.html
Copyright © 2020-2023  润新知