欢迎访问移动开发之家(rcyd.net),关注移动开发教程。移动开发之家  移动开发问答|  每日更新

Android开发日志统一管理,android统一管理

来源: 开发者 投稿于  被查看 49512 次 评论:155

Android开发日志统一管理,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 + "\r\n");
            } else {
                sb.append(e + "\r\n");
            }
            if (sts != null && sts.length > 0) {
                for (StackTraceElement st : sts) {
                    if (st != null) {
                        sb.append("[ " + st.getFileName() + ":" + st.getLineNumber() + " ]\r\n");
                    }
                }
            }
            android.util.Log.e(TAG, sb.toString());
        }
    }
}

 

用户评论