Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable some Log functions when building an Android app?

I use Log functions to debug my Android app. I'm close to releasing my app and I'd like to reduce debug level. However I don't know how to disable some of these function. Say I just want to disable Log.v() logs.

Android doc says here that one should remove Log functions calls in code or remove/set to false the android::debbugable option. The first option does not fit my needs, since I'd like to Log calls in my source code (for future updates). The second option disables all logs, which is not convenient for pre-release testing.

Do you know how to disable only verbose (Log.v) logs ?

Many thanks!

like image 259
Arnaud Courtecuisse Avatar asked Feb 21 '23 22:02

Arnaud Courtecuisse


2 Answers

You could create your own wrapper for the Log-class, maybe something like this:

static class MyLog{
    private static int mLevel;
    public final static void setLevel(int level){
        mLevel = level;
    }
    public final static void v(String tag, String message){
        if( mLevel > 0 ) return;
        Log.v(tag, message);
    }
    public final static void d(String tag, String message){
        if( mLevel > 1 ) return;
        Log.d(tag, message);
    }
    public final static void i(String tag, String message){
        if( mLevel > 2 ) return;
        Log.d(tag, message);
    }
    //Same for w and e if neccessary..
}

Then you can use MyLog.setLevel(1); to disable any verbose logging (or a higher level to disable those logs as well).

like image 195
Jave Avatar answered Mar 29 '23 23:03

Jave


If you are going to obfuscate your release with Proguard, then putting lines such as

-assumenosideeffects class android.util.Log {
    public static *** d(...);
    public static *** v(...);
}

into the config file will remove those categories of logging from your code.

like image 29
NickT Avatar answered Mar 29 '23 23:03

NickT