Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

android

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.

like image 611
michael Avatar asked Nov 10 '11 23:11

michael


3 Answers

Leaving logging statements can, in some cases, be very bad: http://web.archive.org/web/20121222023201/http://vbsteven.com/archives/535

You can use ProGuard to automatically remove them all:

-assumenosideeffects class android.util.Log {
    public static *** d(...);
    public static *** e(...);
}
like image 66
Krylez Avatar answered Nov 15 '22 22:11

Krylez


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

like image 28
Reno Avatar answered Nov 15 '22 20:11

Reno


Not only can the user (or any app with log access) see your logs, but your logs also cause your app to run slower and use more battery power.

Writing a log, especially from Java, is probably much more expensive than you think: just building the string in Java is a lot of work.

like image 29
hackbod Avatar answered Nov 15 '22 21:11

hackbod