Log4j and AOP, how to get actual class name

后端 未结 3 1081
再見小時候
再見小時候 2021-02-05 19:59

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... i

3条回答
  •  无人及你
    2021-02-05 20:14

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

提交回复
热议问题