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

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

 @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; } 

这更容易:

 pjp.getTarget().getClass() 

使用pjp.getTarget().getClass().getCanonicalName()

另外,要在类级别添加日志,而不是使用通知类的记录器,请使用通知类的记录器,在下面使用类级别记录器,如下所示

 Logger logger = Logger.getLogger(pjp.getTarget().getClass().getCanonicalName());