logo

Everyone writes logs during project development, as logs are really helpful for debugging applications, displaying exceptions or errors. But this can lead to headache if you forgot to remove those logs at the end of 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 release of our app?

Clean way to write solution for 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.

Lets have a look how to use these-

First of all we have to write 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

Log.d("AppName","This is simple way to display logs");

And for logging error, simply call-

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 debug.

BuildConfig.DEBUG=false

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

AUTHOR: Vikas Hiran
10 Comments
  • Heya i am for the first time here. I came across this
    board and I find It really useful & it helped me out much.
    I hope to give something back and help others like you helped me.

    December 23, 2016
  • I think that is among the most important information for me.
    And i am satisfied reading your article. However
    should remark on few general issues, The website style is perfect, the articles is really excellent : D.

    Excellent job, cheers

    January 26, 2017
  • My family always say that I am wasting my time here at web, however
    I know I am getting familiarity every day by reading thes good content.

    January 28, 2017
  • Fantastic blog! Do you have any suggestions for aspiring writers?
    I’m hoping to start my own blog soon but I’m a little lost on everything.
    Would you advise starting with a free platform like
    Wordpress or go for a paid option? There are so many options out there that I’m completely
    overwhelmed .. Any ideas? Kudos!

    February 4, 2017
  • This is a topic which is close to my heart… Thank you!
    Exactly where are your contact details though?

    February 8, 2017

Leave a Comment

Your email address will not be published.