Log4j和AOP,如何获取实际的类名

2022-09-04 06:45:05

我正在使用Spring AOP和Log4J实现记录器作为一个方面,但我注意到日志文件中的类名始终是类名,所以...有没有办法在我的日志中跟踪实际的类名?LoggerAspect


答案 1
@Around("execution(* com.mycontrollerpackage.*.*(..))")
public Object aroundWebMethodE(ProceedingJoinPoint pjp) throws Throwable {      
    String packageName = pjp.getSignature().getDeclaringTypeName();
    String methodName = pjp.getSignature().getName();
    long start = System.currentTimeMillis();
    if(!pjp.getSignature().getName().equals("initBinder")) {
       logger.info("Entering method [" + packageName + "." + methodName +  "]");
    }
    Object output = pjp.proceed();
    long elapsedTime = System.currentTimeMillis() - start;
    if(!methodName.equals("initBinder")) {
       logger.info("Exiting method [" + packageName + "." + methodName + "]; exec time (ms): " + elapsedTime);
    }
    return output;
}

答案 2

这更容易:

pjp.getTarget().getClass()

推荐