Everyone writes logs during project development, as logs are really helpful for debugging applications, displaying exceptions or errors. But this can lead to a headache if you forgot to remove those logs at the end of the project, as many of us don’t want to show our apps API calls and its responses to the world for security reasons. 🙂

So how can we get rid of these logs during the release of our app?

A clean way to write a solution for the above problem will be,

  1. Use BuildConfig.DEBUG variable – which is automatically generated by the android build, by default set to true which means it is in debug mode and false when you export a release build.
  2. Wrap in helper SimpleLog class which will display logs only when we are debugging.

Let's have a look at how to use these-

First of all, we have to write a custom class like below one

public class SimpleLog{

public static void log(String TAG, String message) {
if (BuildConfig.DEBUG) {
Log.d(TAG, message);
}
}

public static void logError(String TAG, String message) {
if (BuildConfig.DEBUG) {
Log.e(TAG, message);
}
}

public static void logException(String TAG, Exception exe) {
if (BuildConfig.DEBUG) {
Log.e(TAG, exe.getMessage());
exe.printStackTrace();
}
}
}

After our class is ready we have to call,  

SimpleLog.log(“AppName”,”This is simple way to display logs”); 

 instead of  

SimpleLog.logError(“AppName”,”Error”);  

and that’s how we simplified a way to write logs. 🙂 

Note:  

BuildConfig.DEBUG=true  

when apps build variant will be debugged.  

BuildConfig.DEBUG=false  

when apps build variant will be released, so no logs will be displayed. 🙂

Do you have any product idea or business need?

How TO MAKE YOUR APP WORK OFFLINE

HOW TO MAKE YOUR APP WORK OFFLINE

Offline mobile app development is critical for users to sync their data properly, when offline. Here, we help you learn the process from implementation to data synchroniz

Omnivore POS integration - Ensuring Agility To Your Restaurant Businesses

Omnivore POS integration - Ensuring Agility To Your Restaurant Businesses

Omnivore software offers a point-of-sales integration API making the restaurant system agile, more customer engaging and adaptive to fast changing business environments.

Unit Testing using Mockk.io

Unit Testing using mockK.io in Kotlin

Learn about unit testing using mockk.io in Kotlin.