Skip to content

Instantly share code, notes, and snippets.

@gooooloo
Created August 19, 2016 06:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gooooloo/2dde437504408f79a88f5fa391d0810c to your computer and use it in GitHub Desktop.
Save gooooloo/2dde437504408f79a88f5fa391d0810c to your computer and use it in GitHub Desktop.
get JAVA Line info for log.
package info.gooooloo;
public class MyLineInfo {
public static String info() {
final StackTraceElement element = new Exception().getStackTrace()[1];
return String.format("%s#%s,%s:%d",
element.getClassName(),
element.getMethodName(),
element.getFileName(),
element.getLineNumber());
}
static class AAA {
public void foo() {
System.out.println(MyLineInfo.info());
}
}
public static void main(String[] args) {
System.out.println(MyLineInfo.info());
System.out.println(MyLineInfo.info());
new AAA().foo();
System.out.println(MyLineInfo.info());
System.out.println(MyLineInfo.info());
System.out.println(MyLineInfo.info());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment