LogUtils工具类

/**
 * @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) {
        }
    }
}

测试类

public static void main(String[] args) {
   Runnable runnable = () -> LogUtils.enter();
   runnable.run();
}

LogUtils工具类_第1张图片

你可能感兴趣的:(spring,java,后端)