Skip to content

Instantly share code, notes, and snippets.

@JamesWang
Created September 11, 2010 02:13
Show Gist options
  • Save JamesWang/574690 to your computer and use it in GitHub Desktop.
Save JamesWang/574690 to your computer and use it in GitHub Desktop.
public static String formatWithStackInfo(String message) {
/**
* ex.getStackTrace()[0] is this formatWithStackInfo() method ex.getStackTrace()[1]
* is your method where you call this method
*/
StackTraceElement ste = new Throwable().getStackTrace()[1];
String callerClassName = ste.getClassName();
int callerLineNumber = ste.getLineNumber();
String methodName = ste.getMethodName();
callerClassName = callerClassName.substring(callerClassName
.lastIndexOf(".") + 1);
return "[" + callerClassName +"." + methodName + ":" + callerLineNumber + "]" + message;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment