Logs are the main means for us to troubleshoot online problems. For online access performance statistics, AOP methods are usually used to count the time-consuming methods. Recently, I encountered a problem in the project. An old project using log4j to add performance statistics logs. The methods are very common:
The aspect of intercepting logs is defined as follows:
@Component @Aspect public class TimeLogIntercept { private final Logger logger = LoggerFactory. getLogger (TimeLogIntercept. class ) ; /** * All public methods of all serviceClass classes */ @Pointcut ( "execution(public * com.test..*ServiceImpl.*(..)) " ) public void serviceClass () { } /** * All public methods of all serviceClass classes */ @Pointcut ( "execution(public * com.test..*Dao.*(..))" ) public void daoClass () { } /** * All public methods of all controller classes */ @Pointcut ( "execution(public * com.test.web..*Controller.*(..))" ) public void controllerClass () { } @Around("serviceClass() || daoClass() || controllerClass()") public Object around(ProceedingJoinPoint pjp) throws Throwable { Long startTime = 0L; String methodName = ""; try { startTime = System.currentTimeMillis(); MethodSignature joinPointObject = (MethodSignature) pjp.getSignature(); Method method = joinPointObject.getMethod(); methodName = pjp.getTarget().getClass().getName(); methodName = methodName + "." + method.getName(); Object proceed = pjp.proceed(); return proceed; } finally { Long endTime = System.currentTimeMillis(); long time = endTime - startTime; if (time > 1000) { logger.warn(" cost time:" + time + "ms." + "methodName is ->" + methodName); }
logger.warn(" cost time:" + time + "ms." + "methodName is ->" + methodName);
} } } The configuration of log4j is as follows:
# performanceFilter log4j.appender.PA=org.apache.log4j.DailyRollingFileAppender log4j.appender.PA.file=../logs/wf_server_performance.log log4j.appender.PA.DatePattern='.'yyyy-MM-dd log4j.appender.PA.layout=org.apache.log4j.PatternLayout log4j.appender.PA.layout.ConversionPattern=[%p]\t%d\t[%t]\t%c{3}\t(%F\:%L)\t-%m%n log4j.appender.PA.bufferSize=10000 log4j.logger.com.test.TimeLogInterceptor (1) If there is a problem with the Logger object, the Logger object of log4j should be used. The correct method is Logger log = Logger.getLogger(TimeLogInterceptor.class); After changing the above, it is found that the log is still not printed, so the problem should be on AOP Check the Spring configuration and find that there is a problem with the AOP configuration. The original old project configuration did not use the AOP aspect, so the aspect-related configuration was added: Add the schema file declaration: xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd" Also need to add:
<aop:aspectj-autoproxy />
After the test, it is found that the log can finally be output normally, and the problem is solved!