zoukankan      html  css  js  c++  java
  • 移动开发中的日志管理

            在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() + "
    At line:" + st.getLineNumber()
                // + "; Method:" + st.getMethodName();
            }
            return null;
        }
    }
  • 相关阅读:
    [翻译] DBCamera 轻量级定制摄像头
    使用正则表达式寻找字符串中出现了几个[***]样式的字符串
    [转] iOS文字排版(CoreText)那些事儿
    辉光的UIView
    html5全局属性
    meta你到底了解多少
    原生APP与移动Web App的比较
    Web App开发入门
    web移动开发最佳实践之html篇
    MVC中的扩展点(六)ActionResult
  • 原文地址:https://www.cnblogs.com/magics/p/3820052.html
Copyright © 2011-2022 走看看