AOP切面记录日志,并计算请求接口耗时 男娘i 2022-12-06 01:17 107阅读 0赞 借鉴:[https://blog.csdn.net/vtopqx/article/details/79917305][https_blog.csdn.net_vtopqx_article_details_79917305] package com.mortals.iot.framework.aspect; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import com.mortals.framework.ap.SysConstains; import com.mortals.framework.util.HttpUtil; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.*; import org.springframework.stereotype.Component; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import lombok.extern.slf4j.Slf4j; @Aspect @Slf4j @Component public class AccessLogAspect { @Pointcut("execution(public * com.xxx.iot..*Controller.*(..))") public void accessLog() { } /* @Before("accessLog()") public void doBefore(JoinPoint joinPoint) { ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest request = attributes.getRequest(); // url log.info("ip[{}]url[{}]", request.getRemoteAddr(), request.getRequestURL()); // ags if (joinPoint != null && joinPoint.getArgs() != null) { StringBuffer buf = new StringBuffer("["); for (int i = 0; i < joinPoint.getArgs().length; i++) { Object arg = joinPoint.getArgs()[i]; if (arg != null && !(arg instanceof ServletRequest) && !(arg instanceof ServletResponse)) { String argStr = arg.toString(); //针对登录密码日志,做特殊处理 if (argStr.contains("password") && argStr.contains("LoginForm")){ int pwIndex = argStr.indexOf("password="); argStr = argStr.substring(0,pwIndex+10)+"****"+argStr.substring(argStr.indexOf("'",pwIndex+11)); } buf.append(argStr); } } log.info("args{}", buf.append("]").toString()); } }*/ /* @AfterReturning(returning = "object", pointcut = "accessLog()") public void doAfterReturning(Object object) { if (null != object) { log.info("response={}", object.toString()); } }*/ @Around("accessLog()") public Object around(ProceedingJoinPoint joinPoint){ long startTime = System.currentTimeMillis(); // 定义返回对象、得到方法需要的参数 Object resultData = null; Object[] args = joinPoint.getArgs(); String uri = ""; String methodName = joinPoint.getSignature().getName(); try { ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest request = attributes.getRequest(); uri = request.getServletPath(); // log.info("ip[{}]url[{}]", request.getRemoteAddr(), request.getRequestURL()); String loginName = HttpUtil.getCookieValue(request, SysConstains.COOKIE_PIN); // log.info("用户:" + loginName + ";访问接口:" + uri); log.info("======>用户:{},请求[{}]接口", loginName, request.getServletPath()); if (joinPoint != null && joinPoint.getArgs() != null) { StringBuffer buf = new StringBuffer("["); for (int i = 0; i < joinPoint.getArgs().length; i++) { Object arg = joinPoint.getArgs()[i]; if (arg != null && !(arg instanceof ServletRequest) && !(arg instanceof ServletResponse)) { String argStr = arg.toString(); //针对登录密码日志,做特殊处理 if (argStr.contains("password") && argStr.contains("LoginForm")){ int pwIndex = argStr.indexOf("password="); argStr = argStr.substring(0,pwIndex+10)+"****"+argStr.substring(argStr.indexOf("'",pwIndex+11)); } buf.append(argStr); } } log.info("args{}", buf.append("]").toString()); } resultData = joinPoint.proceed(args); long endTime = System.currentTimeMillis(); log.info("======>用户:{},完成请求[{}],耗时:{},返回:{}", loginName, uri, (endTime - startTime), resultData.toString()); } catch (Throwable e) { e.printStackTrace(); // 记录异常信息 long endTime = System.currentTimeMillis(); log.error("======>请求[{}]异常!耗时:{}", uri, (endTime - startTime)); } return resultData; } } [https_blog.csdn.net_vtopqx_article_details_79917305]: https://blog.csdn.net/vtopqx/article/details/79917305
还没有评论,来说两句吧...