5

Update: Reformulated the question and header:

I always thought that expensive android Logging methods can be optimized by asking if logging is active like this

    import android.util.Log;

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

However when trying 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

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

Is there an other way to find out if debugging (or one of the other loglevels) is active or not?

I use eclipse logcat-view with log-level verbose and started the test from eclipse with run as android-app

k3b
  • 14,517
  • 7
  • 53
  • 85
  • Was this during a simple Run As Android Application or Debug As Android Application? – John Giotta Feb 27 '12 at 16:31
  • the output is from "Run As Android Application". Is there any difference between both? – k3b Jan 17 '13 at 11:20
  • The result is the same for Debug As Android Application, "DEBUG logging is active: false" – Goo Jan 17 '13 at 12:52
  • 2
    Same problem here http://stackoverflow.com/q/7948204/1720391 – Goo Jan 17 '13 at 12:56
  • 2
    Just a side note: a more elegant and strategic way is to [use Proguard remove all log call](http://stackoverflow.com/questions/12390466/android-proguard-not-removing-all-log-messages/12396486#12396486) at project release phase. – yorkw Jan 22 '13 at 21:14

1 Answers1

3

isLoggable is really just a mechanism to provide a flag for certain tags for your convenience. It doesn't actually do anything to disable logging. Your first block of code is correct:

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

This will log or not 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 log regardless of what isLoggable would have returned had you called it. In short, you need to do that check everywhere that you log if you want to enable/disable logging based on that flag. The if statement is the part that lets you skip unnecessary logs. If the if clause is false, the code inside is never run.

kabuko
  • 36,028
  • 10
  • 80
  • 93
  • I'm still confused as to how this works: Shouldn't `Log.isLoggable("MyContext", Log.DEBUG)` return `false` by default as the docs say "The default level of any tag is set to INFO. This means that any level above and including INFO will be logged"? Yet it returns `true` for me. – sschuberth Jul 16 '15 at 08:14
  • This confuses me. Why doesn't Log.d() check the log level internally? In other logging frameworks something like "if(logLevelEnabled(Loglevel.DEBUG))" is only useful so you don't waste performance on creating the log message. But that is even a param of Log.isLoggable. – Heinzlmaen Aug 14 '19 at 10:48