当我为我的
Android应用程序编写一个日志包装时,我注意到一个奇怪的行为,Android的Log.isLoggable方法.执行以下代码:
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));
产生以下LogCat输出:
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
解决方法
无论当前日志级别是什么,所有日志级别都将写入到logcat. isLogabble()方法可以用作您的应用程序的优化,以防止将不需要的日志语句发送到logcat.您也可以使用adb logcat命令过滤日志级别的子集,即使日志记录设置为详细(参见
https://developer.android.com/studio/debug/am-logcat.html).