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"); } }
时间: 2024-10-08 10:05:23