public static void LogD(String tag, String msg) {
StackTraceElement ste = new Throwable().getStackTrace()[1];
String s = "[" + ste.getClassName() + "." + ste.getMethodName() + "(" + ste.getLineNumber() + ")] " ;
Log.d(tag, s + msg);
}
In Android it is handled by the Log class. It too allows for runtime configuration of loggers and it involves setting Android system properties using the setprop command, which is nice if you are building an app as part of the OS but maybe overkill or even clunky if you just want to create your next great app. And what if you don't want your shipping app to have any logging whatsoever? You certainly want it during development, trust me :) Well enter this little utility class:
Now the beauty of this all is that since the LOGD variable is a static final (a constant) the compiler will strip out the entire if statement if the LOGD variable is false, hence no logging in your final app and no bytecode generated for it either.
So by introducing a simple class and wrapping the log statements in an if block you'll still be able to enable particular levels of logging, while in your shipping product the logging is stripped out.