Error or function in android.util.Log? - Log.isLoggable (DEBUG) = false, but Log.d () is not disabled

Update: reorganized question and title:

I always thought that expensive Android logging methods could be optimized by asking if logging is active in this way.

import android.util.Log; if (Log.isLoggable("MyContext", Log.DEBUG)) { Log.d("MyContext", "my logging: " + callExpensiveCalculation()); } 

However, when I try to use this with the Android 2.2 emulator, my Log.d () is never called.

So I tried this code

  Log.v(MY_CONTEXT, "VERBOSE logging is active: " + Log.isLoggable(MY_CONTEXT, Log.VERBOSE)); Log.d(MY_CONTEXT, "DEBUG logging is active: " + Log.isLoggable(MY_CONTEXT, Log.DEBUG)); Log.i(MY_CONTEXT, "INFO logging is active: " + Log.isLoggable(MY_CONTEXT, Log.INFO)); Log.w(MY_CONTEXT, "WARN logging is active: " + Log.isLoggable(MY_CONTEXT, Log.WARN)); Log.e(MY_CONTEXT, "ERROR logging is active: " + Log.isLoggable(MY_CONTEXT, Log.ERROR)); 

and to my surprise I got

 02-27 19:05:43.015: V/MyContext(334): VERBOSE logging is active: false 02-27 19:05:43.015: D/MyContext(334): DEBUG logging is active: false 02-27 19:05:43.015: I/MyContext(334): INFO logging is active: true 02-27 19:05:43.015: W/MyContext(334): WARN logging is active: true 02-27 19:05:43.015: E/MyContext(334): ERROR logging is active: true 

therefore, logging works even if logging is disabled. Is this a bug in android or in my test code?

Is there any other way to find out if debugging is active (or one of the other loglevel levels)?

I am using eclipse logcat-view with detailed logic level and started checking with eclipse using asroid-app

+4
source share
1 answer

isLoggable is just a mechanism for providing a flag for specific tags for your convenience. In fact, it does nothing to disable logging. Your first block of code is correct:

 if (Log.isLoggable("MyContext", Log.DEBUG)) { Log.d("MyContext", "my logging: " + callExpensiveCalculation()); } 

This will be a log or not a log, depending on if isLoggable returns true or false . However, when you do this (without checking isLoggable ):

 Log.d(MY_CONTEXT, "DEBUG logging is active: " + Log.isLoggable(MY_CONTEXT, Log.DEBUG)); 

It will be logged regardless of what isLoggable would return if you called it. In short, you need Perform this check wherever you register if you want to enable / disable logging based on this flag. The if is the part that lets you skip unnecessary logs. If the if clause if false , the code inside never runs.

+3
source

Source: https://habr.com/ru/post/1398637/


All Articles