8

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.

roottraveller
  • 7,942
  • 7
  • 60
  • 65
michael
  • 106,540
  • 116
  • 246
  • 346

8 Answers8

7

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(...);
}
Krylez
  • 17,414
  • 4
  • 32
  • 41
  • 1
    The blog post you refer to was removed, along with the entire blog. The author said this: "So I decided to start over with a clean slate. New content, new blogging engine and a new design" – Tjaart Mar 19 '13 at 09:44
  • 1
    Thanks, I replaced the link with an archived version. – Krylez Mar 19 '13 at 16:48
  • Thanks. I am learning more about logging before releasing my application. The post was very helpful. – Tjaart Mar 20 '13 at 06:58
4

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.

Reno
  • 33,594
  • 11
  • 89
  • 102
hackbod
  • 90,665
  • 16
  • 140
  • 154
  • Thank you. I read this http://android-developers.blogspot.com/2010/09/proguard-android-and-licensing-server.html in how to use Proguard in my build.xml file for android project. But it does not say how to use Proguard to strip all calls to Log.d and Log.e. Can you please tell me how can I do that? – michael Nov 11 '11 at 07:21
4

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

Community
  • 1
  • 1
Reno
  • 33,594
  • 11
  • 89
  • 102
  • Thanks. Can you please help us understand the usage of isLoggable()? I have code like this Log.d(MYTAG, "debug statement"); To use isLoggable() does that mean I need to do 'if isLoggable(MYTAG, Log.DEBUG) { Log.d(MYTAG, "debug statment"); ' every where? – michael Nov 11 '11 at 07:06
  • I've modified my answer. Krylez's solution is the best, this is just an alternative. – Reno Nov 11 '11 at 07:21
1

As it is said in android.developer.com

when you're building the string to pass into Log.d, the compiler uses a StringBuilder and at least three allocations occur: the StringBuilder itself, the buffer, and the String object. Realistically, there is also another buffer allocation and copy, and even more pressure on the gc. That means that if your log message is filtered out, you might be doing significant work and incurring significant overhead.

here is the link: http://developer.android.com/reference/android/util/Log.html

Masoud Dadashi
  • 1,044
  • 10
  • 11
1

They can be used when developing. When release, it is better to remove or obfuscate the message to protect PII(Personally identifiable information). The answer here is very useful: Android Log.v(), Log.d(), Log.i(), Log.w(), Log.e() - When to use each one?

Here is the one teach you the tool to deal with these logs when release the software.Remove all debug logging calls before publishing: are there tools to do this?

Community
  • 1
  • 1
Phi
  • 85
  • 13
1

There are apps in the Market that allow users to see logs even without hooking up a development environment.

CatLog is one I have personally used and it will show you everything.

What I've done in my app is create a wrapper class that I can turn off myself with a boolean flag. It has the added benefit of having the TAG parameter being common throughput my code.

public class QLog {
    private static final String TAG = "MyAppTag";
    private static final boolean DEBUG_MODE = true;

    public static void d(String msg) {
        if(DEBUG_MODE) {
            Log.d(TAG, msg);
        }
    }
    ...
}
brianestey
  • 8,202
  • 5
  • 33
  • 48
0

Logging guidelines

Log.v(String tag, String msg) (verbose)
Log.d(String tag, String msg) (debug)
Log.i(String tag, String msg) (information)
Log.w(String tag, String msg) (warning)
Log.e(String tag, String msg) (error)

To only show logs on debug builds:

if (BuildConfig.DEBUG) Log.d(TAG, "The value of x is " + x);
Elham Dabiri
  • 435
  • 2
  • 9
0

If your user is a developer, and connects the device to a developing machine and have LogCat opened he will se all logs on Logcat if he interracts with that application. When you are going to publish the application to the final user you should remove those logs.

pharaoh
  • 313
  • 1
  • 3
  • 15