Android Log高級用法

Log Level

我們知道不管在AS還是Eclipse中查看log,選擇不同的Log Level查看到不同的log
只有當前的level比Log Level要大,纔會顯示log,做了過濾

    /**
     * Priority constant for the println method; use Log.v.
     */
    public static final int VERBOSE = 2;

    /**
     * Priority constant for the println method; use Log.d.
     */
    public static final int DEBUG = 3;

    /**
     * Priority constant for the println method; use Log.i.
     */
    public static final int INFO = 4;

    /**
     * Priority constant for the println method; use Log.w.
     */
    public static final int WARN = 5;

    /**
     * Priority constant for the println method; use Log.e.
     */
    public static final int ERROR = 6;

    /**
     * Priority constant for the println method.
     */
    public static final int ASSERT = 7;

Log.isLoggable方法的使用

android 動態控制logcat日誌開關,通過Log.isLoggable(TAG,level)方法動態控制,
1.添加日誌的時候加入判斷

        String TAG="Volley";
        boolean isDbug=Log.isLoggable(TAG, Log.VERBOSE);
        if (isDbug) {
            Log.w(TAG, "log");
        }

2.通過設置屬性值來控制該日誌開關

首先可以通過adb shell getprop log.tag.Volley查看系統屬性值 如果沒有設置是查看不到值的,但是同時默認值是INFO,就是說如果不做任何設置,Log.isLoggable(TAG, Log.VERBOSE);返回的都將是false,因爲VERBOSE等級值比INFO小

通過adb shell setprop log.tag.Volley VERBOSE設置該TAG的輸出級別爲VERBOSE。 那麼說明Log.isLoggable(“Volley”, Log.VERBOSE) level爲VERBOSE以及以上的level的都返回true,又因爲VERBOSE就是最小的一個等級,所以isLoggable返回的始終是true,每設置一次,只能用於一部手機沒有重啓的情況,如果換一部或者重啓要重新設置一下;這樣的好處是,自己開發的手機設置一次,都能打印VERBOSE,編譯給別的手機就不能打印VERBOSE信息,這樣就不用每次正式發佈時要把isDbug設置爲false。該屬性值取值順序爲【V,D,I,W,E,A,S】 A表示最高級別的日誌,即assert;S表示Suppress,即停止該日誌的輸出。

也可以將該屬性添加在data/local.prop屬性文件中,不同的是,只要存在local.prop,該手機重啓與否都一樣,可以打印VERBOSE

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章