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,
- Use
BuildConfig.DEBUG
variable – which is automatically generated by the android build, by default set totrue
which means it is in debug mode andfalse
when you export a release build. - 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. 🙂