移动开发中的日志管理

        在Android移动开发中,日志为我们提供了很多便利。但是应用程序发布后又不想让应用程序输出日志信息,就可以设计一个日志开关对应用中的日志做统一的管理。下面这个简单的日志类就完成了这样的功能,有需要的朋友可以参考。

package com.hitech.jni4cppdemo.utils;



public class Log {



    // 应用名称

    private static final String TAG = "ResXtrojan";

    // 日志开关

    private static boolean switches = false;

    // 日志级别

    private static int level = android.util.Log.VERBOSE;



    public static void v(Object log) {

        if (switches) {

            if (level <= android.util.Log.VERBOSE) {

                if (functionName() != null) {

                    android.util.Log.v(TAG, functionName() + log);

                } else {

                    android.util.Log.v(TAG, log.toString());

                }

            }

        }

    }



    public static void d(Object log) {

        if (switches) {

            if (level <= android.util.Log.DEBUG) {

                if (functionName() != null) {

                    android.util.Log.d(TAG, functionName() + log);

                } else {

                    android.util.Log.d(TAG, log.toString());

                }

            }

        }

    }



    public static void i(Object log) {

        if (switches) {

            if (level <= android.util.Log.INFO) {

                if (functionName() != null) {

                    android.util.Log.i(TAG, functionName() + log);

                } else {

                    android.util.Log.i(TAG, log.toString());

                }

            }

        }

    }



    public static void w(Object log) {

        if (switches) {

            if (level <= android.util.Log.WARN) {

                if (functionName() != null) {

                    android.util.Log.w(TAG, functionName() + log);

                } else {

                    android.util.Log.w(TAG, log.toString());

                }

            }

        }

    }



    public static void e(Object log) {

        if (switches) {

            if (level <= android.util.Log.ERROR) {

                if (functionName() != null) {

                    android.util.Log.e(TAG, functionName() + log);

                } else {

                    android.util.Log.e(TAG, log.toString());

                }

            }

        }

    }



    private static String functionName() {

        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 "at " + st.getFileName() + ", line " + st.getLineNumber()

                    + ", method:" + st.getMethodName() + ", ";

            // return "Thread:" + Thread.currentThread().getName() + "; Source:"

            // + st.getFileName() + "\nAt line:" + st.getLineNumber()

            // + "; Method:" + st.getMethodName();

        }

        return null;

    }

}

 

你可能感兴趣的:(移动开发)