移動開發中的日誌管理

來源:互聯網
上載者:User

        在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;    }}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.