java
/**
* @description:
* @author:
* @create: 2023/10/23 19:52
**/
public class LogUtils {
public static void enter() {
StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
StackTraceElement stackTraceElement = stackTrace[stackTrace.length - 2];
String callerMethodName = stackTraceElement.getMethodName();
// 判断是否不是lambda表达式
if (callerMethodName.startsWith("lambda$")) {
stackTraceElement = stackTrace[stackTrace.length - 1];
callerMethodName = stackTraceElement.getMethodName();
}
try {
Class<?> aClass = ClassUtils.forName(stackTraceElement.getClassName(), null);
LoggerFactory.getLogger(aClass).info("Enter Method: {}", callerMethodName);
} catch (ClassNotFoundException ignored) {
}
}
}
测试类
java
public static void main(String[] args) {
Runnable runnable = () -> LogUtils.enter();
runnable.run();
}