標籤:
package com.someorg;public class Logger { public static void info(String something) { int lineNo = getCurrentLineNumber(); String methodName = getCurrentMethodName(); String className = getCurrentClassName(); System.out.println(className + "." + methodName + "(" + lineNo + "): " + something); } public static int getCurrentLineNumber() { return Thread.currentThread().getStackTrace()[3].getLineNumber(); } public static String getCurrentMethodName() { return Thread.currentThread().getStackTrace()[3].getMethodName(); } public static String getCurrentClassName() { return Thread.currentThread().getStackTrace()[3].getClassName(); }}
package com.someorg;public class App { public static void main(String[] args) { m1(); } public static void m1() { // do some logic here; Logger.info("Hello 1"); m2(); } public static void m2() { // do some logic here; Logger.info("Hello 2"); }}
Java 獲得運行時檔案名稱,類名,方法名,行號