Does Log.isLoggable returns wrong values?

允我心安 提交于 2019-11-28 07:04:39

问题


When I was writing a log wrapper for my android application I noticed a strange behavior of androids Log.isLoggable method. Executing following code:

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));

produces following LogCat output:

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

Why do I get verbose and debug is not active although I produce these outputs using verbose and debug logging?


回答1:


All log levels are written to logcat regardless of what the current log level is. The isLogabble() method can be used as an optimization for your apps to prevent sending unneeded log statements to logcat. You can also use the adb logcat command to filter a subset of the logging levels even if the logging is set to verbose (see https://developer.android.com/studio/debug/am-logcat.html).




回答2:


If you read the information on Log.isLoggable(), you'll notice that the default logging level is INFO. Anything less than that (DEBUG and VERBOSE) will cause this method to return false. This is why your resulting output shows these two as false.

All Log.* calls are logged to the logcat. Calling Log.isLoggable() is simply a way for you to tune logging. It's not required. Typically, you'll call Log.isLoggable() prior to the actual Log.* call to determine whether to log it or not.

You can tune your logging per TAG if you choose, either with a prop file or via adb. The nice thing about this is you can dynamically turn logging up/down/off for each individual TAG in your app without the need for manually commenting out Log lines, or implementing your own checks.




回答3:


You should use

if (Log.isLoggable(TAG, Log.VERBOSE)) {
    Log.v(TAG, "verbose is active: " + Log.isLoggable(TAG, Log.VERBOSE));
}



回答4:


LOG will always be print in the logcat no matter what Log.isloggable() return.



来源:https://stackoverflow.com/questions/7948204/does-log-isloggable-returns-wrong-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!