-3

I need to print logs in my application only while I am developing it. So is there a way like a boolean which will decide if my app is in a certain mode , and only allows to print log then? This will save my time from deleting logs every time I prepare for a signed build.

I tried solutions like:

boolean isDebuggable = 0 != (getApplicationInfo().flags &= ApplicationInfo.FLAG_DEBUGGABLE);

and:

if (BuildConfig.DEBUG) {
  // do something for a debug build
}`

They don't work like I want it to.

Thank you.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Pritish
  • 1,284
  • 1
  • 19
  • 42

4 Answers4

5

If you are using Android Studio, then try to take benefit of gradle file.

  defaultConfig {
        applicationId "com.your.application.id"
        minSdkVersion 14
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"

        //Defining Log debugging
        buildConfigField "boolean", "LOG_DEBUG_MODE", "false"
        buildConfigField "boolean", "LOG_DEBUG_WITH_STACKTRACE_MODE", "false"
    }


    buildTypes {
        debug {
            testCoverageEnabled = "true"
            buildConfigField "boolean", "LOG_DEBUG_MODE", "true"
            buildConfigField "boolean", "LOG_DEBUG_WITH_STACKTRACE_MODE", "true"
        }

        release {
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
Aman Shekhar
  • 2,719
  • 1
  • 18
  • 29
3

Just use a static variable debug like this:

if(debug){
//do something
}

And set debug to false when you release the apk file.

Or create your own custom log method:

public static void logInfo(String tag, String msg){
    if(DEBUG){
        Log.i(tag,msg);
    }
}
TOP
  • 2,574
  • 5
  • 35
  • 60
1

Either way you can use static single method for printing log like shown below:

/**
 * @param TAG
 * @param Message
 * @param LogType
 */
public static void Log(String TAG, String Message, int LogType) {
    switch (LogType) {
        // Case 1- To Show Message as Debug
        case 1:
            Log.d(TAG, Message);
            break;
        // Case 2- To Show Message as Error
        case 2:
            Log.e(TAG, Message);
            break;
        // Case 3- To Show Message as Information
        case 3:
            Log.i(TAG, Message);
            break;
        // Case 4- To Show Message as Verbose
        case 4:
            Log.v(TAG, Message);
            break;
        // Case 5- To Show Message as Assert
        case 5:
            Log.w(TAG, Message);
            break;
        // Case Default- To Show Message as System Print
        default:
            System.out.println(Message);
            break;
    }
}

public static void Log(String TAG, String Message) {
    AppDelegate.Log(TAG, Message, 1);
}

/* Function to show log for error message */
public static void LogD(String Message) {
    AppDelegate.Log(Tags.DATE, "" + Message, 1);
}

/* Function to show log for error message */
public static void LogDB(String Message) {
    AppDelegate.Log(Tags.DATABASE, "" + Message, 1);
}

/* Function to show log for error message */
public static void LogE(Exception e) {
    if (e != null) {
        AppDelegate.LogE(e.getMessage());
        e.printStackTrace();
    } else {
        AppDelegate.Log(Tags.ERROR, "exception object is also null.", 2);
    }
}

/* Function to show log for error message */
public static void LogE(OutOfMemoryError e) {
    if (e != null) {
        AppDelegate.LogE(e.getMessage());
        e.printStackTrace();
    } else {
        AppDelegate.Log(Tags.ERROR, "exception object is also null.", 2);
    }
}

/* Function to show log for error message */
public static void LogE(String message, Exception exception) {
    if (exception != null) {
        AppDelegate.LogE("from = " + message + " => "
                + exception.getMessage());
        exception.printStackTrace();
    } else {
        AppDelegate.Log(Tags.ERROR, "exception object is also null. at "
                + message, 2);
    }
}

/**
 * Funtion to log with tag RESULT, you need to just pass the message.
 *
 * @String Message = pass your message that you want to log.
 */
public static void LogR(String Message) {
    AppDelegate.Log(Tags.RESULT, "" + Message, 1);
}

/**
 * Funtion to log with tag RESULT, you need to just pass the message.
 *
 * @String Message = pass your message that you want to log.
 */
public static void LogI(String Message) {
    AppDelegate.Log(Tags.INTERNET, "" + Message, 1);
}

And after that you just need to write for log in app like :

AppDelegate.LogT("Hello for testing purpose");

and when you don't want to show log then go to AppDelegate class and just comment out the log line. That's it. I hope you understand.

bharat7777
  • 323
  • 3
  • 15
1

You can even use Timber as a third party library. You don't need to declare TAG everytime in your classes and set when it shows (Debug/Release) mode.

implementation 'com.jakewharton.timber:timber:4.7.1'

if (BuildConfig.DEBUG) {
    Timber.plant(new Timber.DebugTree());
}
k4dima
  • 6,070
  • 5
  • 41
  • 39
Alireza Nezami
  • 515
  • 4
  • 12