michael michael - 4 months ago 18
Android Question

Using Log.d() or Log.e() in my code

I have used

Log.d()
and
Log.e()
through out my android application for debugging purpose.
I would like to know if I release my application like this, will the user sees all those debug statement that I put in? Do I need to do something special so that user won't see the debug log even if they hook up 'adb logcat'?

Thank you.

Answer

Consider using this : isLoggable()

Checks to see whether or not a log for the specified tag is loggable at the specified level. The default level of any tag is set to INFO. This means that any level above and including INFO will be logged. Before you make any calls to a logging method you should check to see if your tag should be logged. You can change the default level by setting a system property: setprop log.tag.<YOUR_LOG_TAG> <LEVEL> Where level is either VERBOSE, DEBUG, INFO, WARN, ERROR, ASSERT, or SUPPRESS. SUPPRESS will turn off all logging for your tag. You can also create a local.prop file that with the following in it: log.tag.<YOUR_LOG_TAG>=<LEVEL> and place that in /data/local.prop

Personally for releases, I'd remove the debug logs and keep the error logs using Proguard.


It is better to wrap it like so:

public class MyLog {

public static void d(String tag, String msg) {
   if (Log.isLoggable(tag, Log.DEBUG)) {
       Log.d(tag, msg);
}
}

public static void i(String tag, String msg) {
    if (Log.isLoggable(tag, Log.INFO)) {
     Log.i(tag, msg);
}
}  // and so on... 

You can set logging level by issuing an adb command

Comments