views:

3990

answers:

5
+12  Q: 

Android logging

I am having lots of logging statements to debug for example.

Log.v(TAG, "Message here");
Log.w(TAG, " WARNING HERE");

while deploying this application on device phone i want to turn off the verbose logging from where i can enable/disable logging.

+4  A: 
Christopher
do it contains any log.property file where we can define settings.
Faisal khan
+9  A: 

The Android Documentation says the following about Log Levels:

Verbose should never be compiled into an application except during development. Debug logs are compiled in but stripped at runtime. Error, warning and info logs are always kept.

So you may want to consider stripping the log Verbose logging statements out, possibly using ProGuard as suggested in another answer.

According to the documentation you can configure logging on a development device using System Properties. The property to set is log.tag.<YourTag> and it should be set to one of the following values: VERBOSE, DEBUG, INFO, WARN, ERROR, ASSERT, or SUPPRESS. More information on this is available in the documentation for the isLoggable() method.

You can set properties temporarily using the setprop command. For example:

C:\android>adb shell setprop log.tag.MyAppTag WARN
C:\android>adb shell getprop log.tag.MyAppTag
WARN

Alternatively, you can specify them in the file '/data/local.prop' as follows:

log.tag.MyAppTag=WARN

This file as read at boot time so you'll need to restart after updating it.

Finally, you can set them programmatically using the System.setProperty() method.

However, I've been unable to get these log settings working as documented. I'd be interested to hear if you have any more luck than me.

Dave Webb
I've had the same experience; the API docs are pretty unclear about exactly how this should work and even seem to mention most of the `android.util.Config` constants being deprecated. The hardcoded values specified in the API docs are useless as these (supposedly) vary by build. Hence the ProGuard route seemed the best solution for us.
Christopher
+4  A: 

Common way is make a int named loglevel, and you can define it's debug level based on loglevel .

public static int LOGLEVEL = 1;
public static boolean WARN = LOGLEVEL > 1;
public static boolean DEBUG = LOGLEVEL > 0;
...
    if (DEBUG) Log.v(TAG, "Message here");
    if (WARN) Log.w(TAG, "WARNING HERE");

Later, you can just change the LOGLEVEL for all debug output level.

Cytown
+2  A: 

You should use

    if (Log.isLoggable(TAG, Log.VERBOSE)) {
        Log.v(TAG, "my log message");
    }
dtmilano