13

Android documentation ( http://developer.android.com/reference/android/util/Log.html ) says:

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

I just did a test. In my activity I wrote:

private static String test(String what) {
    Log.e("test", "I am called with argument: " + what);
    return what;
}
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Log.v("test", "log level: " + test("v"));
    Log.d("test", "log level: " + test("d"));
    Log.i("test", "log level: " + test("i"));
    Log.w("test", "log level: " + test("w"));
    Log.e("test", "log level: " + test("e"));
}

I exported my project as an apk file, then I installed this apk on my phone. I run this application on my phone, then I looked at logs. There I saw that the function test was called all five times and all five calls to Log.something functions resulted in its text being written to logs.

So are Log.d calls really stripped at runtime?

user983447
  • 1,598
  • 4
  • 19
  • 36
  • 1
    In short the answer seems to be no. The topic is widely discussed here http://stackoverflow.com/questions/2446248/remove-all-debug-logging-calls-before-publishing-are-there-tools-to-do-this/2466662#2466662 – gpasci Mar 06 '13 at 03:39
  • So it is a bug in the documentation? – user983447 Mar 06 '13 at 05:37
  • So after 5 years, the documentation has not been updated. "You should never compile verbose logs into your app, except during development. Debug logs are compiled in but stripped at runtime, while error, warning, and info logs are always kept." – hjchin Oct 07 '18 at 05:04

3 Answers3

14

No. You have to do this yourself. You can make your own Log.d wrapper like this:

public static void debug(String tag, String msg) {
    if (BuildConfig.DEBUG) {
        Log.d(tag, msg);
    }
}
Saket
  • 2,945
  • 1
  • 29
  • 31
Shellum
  • 3,159
  • 2
  • 21
  • 26
  • 1
    This is the correct answer, so for the record the Android documentation is wrong. You could alternatively also use `Log.isLoggable(TAG, Log.DEBUG)` if you want to enable the logs at the runtime rather than rely on the build type. – satur9nine Dec 15 '16 at 23:58
6

This issue was reported here and a solution was provided in ADT 17.0.0 in Mar 2012

Added a feature that allows you to run some code only in debug mode. Builds now generate a class called BuildConfig containing a DEBUG constant that is automatically set according to your build type. You can check the (BuildConfig.DEBUG) constant in your code to run debug-only functions.

random
  • 10,238
  • 8
  • 57
  • 101
0

The best way to remove logs is probably achieved with ProGuard

Check this question 'Android Proguard, removing all Log statements and merging packages'

Community
  • 1
  • 1
Pascal
  • 15,257
  • 2
  • 52
  • 65