很简单,
通过AOP实现每个方法访问时候统一进行日志打印和耗时计算,
相关配置:
1、spring配置
在spring配置xml文件中设置启用aop
<aop:aspectj-autoproxy proxy-target-class="true" />
2、aop具体业务类(通过注解的方式,使用“环绕通知”)
@Aspect @Component public class LoggingAspect { private static final Log logger = LogFactory.get(); // service层的统计日志/耗时(方法所在的包) public static final String POINT = "execution (* cn.xx.xx.xxxxx.service.impl.*.*(..))"; /** * 统计方法执行耗时Around环绕通知 * @param joinPoint * @return */ @Around(POINT) public Object loggingAround(ProceedingJoinPoint joinPoint) { long startTime = System.currentTimeMillis(); // 定义返回对象、得到方法需要的参数 Object resultData = null; Object[] args = joinPoint.getArgs(); Object apiName = args[0]; try { // 调用钉钉接口 logger.info("======>请求[xxx]接口开始,参数:{}", args); resultData = joinPoint.proceed(args); long endTime = System.currentTimeMillis(); logger.info("======>请求[xxx]接口完成,耗时:{},返回:{}", (endTime - startTime), resultData); } catch (Throwable e) { // 记录异常信息 long endTime = System.currentTimeMillis(); logger.error("======>请求[xxx]接口异常!耗时:{}", (endTime - startTime)); } return resultData; } }配置完成!