0

After seeing a lot of questions and answers about how to disable Log.i/e/d calls on release, the answers vary, either use proguard or a log wrapper which checks a bool value. I actually used a log wrapper myself for quite a while but noticed that on some answers there are created instances of StringBuilder even if the log does not get shown in the end.

So, I am trying the following approach:

public class Logger {

    static final boolean LOG = true;

    public static void printString(String string) {
        if (Logger.LOG) {
            Log.i("MyApp", string);
        }
    }

    public static void printStrings(String string1, String string2) {
        if (Logger.LOG) {
            Log.i("MyApp", string1 + " " + string2);
        }
    }

    public static void printObject(Object object) {
        if (Logger.LOG) {
            Log.i("MyApp", object.toString());
        }
    }

    public static void printException(Exception exception) {
        if (Logger.LOG) {
            Log.i("MyApp", exception.getLocalizedMessage());
        }
    }
}

So my question is, does this implementation have the `StringBuilder issue? I realize that is limited to the number of things to log, but can be extended. My main scope is the be able to easily disable the log messages across the app or have more control over them.

Alin
  • 14,809
  • 40
  • 129
  • 218
  • 1
    `StringBuilder issue` ... what do you mean by that? – Murat Karagöz Sep 16 '16 at 13:33
  • @MuratK. The problem from what I see is when sending messages like "Loged"+ value to the wrapper class, so that is what I am trying to avoid. The issue is described here: http://stackoverflow.com/questions/2446248/remove-all-debug-logging-calls-before-publishing-are-there-tools-to-do-this#comment8849301_4592958 – Alin Sep 16 '16 at 13:37
  • StringBuilder will be created only if you concatenate strings – alex Sep 16 '16 at 13:39
  • @dit so given that the `Log.i("MyApp", string1 + " " + string2);` only happens on LOG=true, this means that StringBuilder will not be created. Right? – Alin Sep 16 '16 at 13:48
  • Yes, your are right. – alex Sep 16 '16 at 13:58

0 Answers0