Skip to content

Instantly share code, notes, and snippets.

@stavfx
Last active December 24, 2015 13:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stavfx/6808390 to your computer and use it in GitHub Desktop.
Save stavfx/6808390 to your computer and use it in GitHub Desktop.
Android Logger class that prints out info about the calling method, just call Log.w("msg") but still get a detailed log and never forget where that log call came from :)
/**
* Created by StavFX on 10/3/13.
*/
public class Log {
private static final boolean DEBUG = true;
private static final String NEW_LINE = System.getProperty("line.separator");
public static void w(String tag, String msg) {
log(tag, msg);
}
public static void w(String msg) {
log(null, msg);
}
private static void log(String tag, String msg) {
if (!DEBUG)
return;
final StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
if (stackTrace.length > 4) {
final StackTraceElement callingMethod = stackTrace[4];
String header;
if (tag != null)
header = String.format("TAG[%s] (%s:%d)=>%s() <==", tag, callingMethod.getFileName(), callingMethod.getLineNumber(), callingMethod.getMethodName());
else
header = String.format("(%s:%d)=>%s() <==", callingMethod.getFileName(), callingMethod.getLineNumber(), callingMethod.getMethodName());
String footer = String.format("%" + (header.length() - 1) + "s>", "=").replace(' ', '=');
msg = String.format("%s%s%s%s%s", header, NEW_LINE, msg, NEW_LINE, footer);
}
System.out.println(msg);
}
}
@sergenes
Copy link

sergenes commented Oct 3, 2013

Cool!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment