天天看点

Android Log.isLoggable

Log.isLoggable

API中关于此方法的说明:

Checks to see whether or not a log for the specified tag is loggable at the specified level. The default level of any tag is set to INFO. This means that any level above and including INFO will be logged. Before you make any calls to a logging method you should check to see if your tag should be logged. You can change the default level by setting a system property: ‘setprop log.tag. ’ Where level is either VERBOSE, DEBUG, INFO, WARN, ERROR, ASSERT, or SUPPRESS. SUPPRESS will turn off all logging for your tag. You can also create a local.prop file that with the following in it: ‘log.tag.=’ and place that in /data/local.prop.

Log.isLoggable()的默认log级别是INFO,像级别低一些的DEBUG和VERBOSE的log就直接被过滤掉了。使用此API,我们可以实现按需求控制log打印:

在正式版本中,像一些DEBUG和VERBOSE的log不会被打印出来,减少了系统打印的log数量

在我们平时调试的时候,可以手动设置log输出级别,

所有应用都采用同样的过滤方式,比设置boolean变量控制方便

final String TAG = "Test";
Log.v(TAG, "verbose is active: " + Log.isLoggable(TAG, Log.VERBOSE));
Log.d(TAG, "debug is active: " + Log.isLoggable(TAG, Log.DEBUG));
Log.i(TAG, "info is active: " + Log.isLoggable(TAG, Log.INFO));
Log.w(TAG, "warn is active: " + Log.isLoggable(TAG, Log.WARN));
Log.e(TAG, "error is active: " + Log.isLoggable(TAG, Log.ERROR));
           

输出

VERBOSE/Test(): verbose is active: false
DEBUG/Test(): debug is active: false
INFO/Test(): info is active: true
WARN/Test(): warn is active: true
ERROR/Test(): error is active: true
           

法的信息可以看出,有2种方式可以修改log过滤级别

  • setprop
adb root

adb shell setprop log.tag.TAG VERBOSE

//在我们的开发板子上面,直接在串口下面输入setprop log.tag.TAG VERBOSE,注意,TAG是我们自己定义的TAG名称
           

但大多數log都不適用,因為它們是在boot時就決定是否要印出log,而此property性質在重開機後就消失

  • 利用local.prop

在linux上,vim local.prop,並寫入 log.tag.TAG=VERBOSE (TAG為依據framework上所設定的TAG, = 要加)

adb root
adb remount
adb push local.prop /data/local.prop
adb shell chmod  /data/local.prop
adb shell chown root.root /data/local.prop
adb reboot
//在开发版上面,直接cp文件到/data
cp XXX/local.prop /data
chmod  /data/local.prop
chown root.root /data/local.prop
reboot
           

参考如下:

http://www.dotblogs.com.tw/kent2480/archive/2014/03/21/144467.aspx

http://stackoverflow.com/questions/7948204/does-log-isloggable-returns-wrong-values