本文介绍了Log4j和AOP,如何获取实际的类名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm implementing a logger as an aspect using Spring AOP and Log4J, but I've noticed that the class name in log file is always the LoggerAspect class name, so... is there a way to trace the actual class name in my log?

推荐答案

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

这篇关于Log4j和AOP,如何获取实际的类名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 09:22