简世博客

一个简单的世界——博客空间,写了一些Android相关的技术文章,和一些点滴的想法

0%

Log.isLoggable使用方式

使用Log.isLoggable可以方便的开关log,但是这个方法具体的用法很多人都不知道.

谷歌注释如下:

public static boolean isLoggable(String tag,
                                 int level)
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.<YOUR_LOG_TAG> <LEVEL>' 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.<YOUR_LOG_TAG>=<LEVEL>' and place that in /data/local.prop.
Parameters:
tag - The tag to check.
level - The level to check.
Returns:
Whether or not that this is allowed to be logged.
Throws:

IllegalArgumentException - is thrown if the tag.length() > 23 for Nougat (7.0) releases (API <= 23) and prior, there is no tag limit of concern after this API level.

这个方法返回一个boolean值,代码里用这个boolean值做判断是否打印log即可.

手机上使用设置prop的方法来动态修改这个boolean.

 

比如我声明的boolean值如下:

static final boolean DEBUG = Log.isLoggable("DozeService", Log.DEBUG);

手机上想让这个值变为true的话,用如下的命令即可

adb shell setprop log.tag.DozeService D