package com.xxxx.modules.ngalain.aop; import com.alibaba.fastjson.JSONObject; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; import org.springframework.web.context.request.RequestAttributes; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.util.Arrays; @Aspect @Component public class LogRecordAspect { private static final Logger logger = LoggerFactory.getLogger(LogRecordAspect.class); // 定义切点Pointcut @Pointcut("execution(public * com.xxxxframework.modules.*..*Controller.*(..))") public void excudeService() { } @Around("excudeService()") public Object doAround(ProceedingJoinPoint pjp) throws Throwable { RequestAttributes ra = RequestContextHolder.getRequestAttributes(); ServletRequestAttributes sra = (ServletRequestAttributes) ra; HttpServletRequest request = sra.getRequest(); String url = request.getRequestURL().toString(); String method = request.getMethod(); Object[] args = pjp.getArgs(); Object[] argArr = Arrays.stream(args).filter(obj -> !(obj instanceof HttpServletRequest) && !(obj instanceof HttpServletResponse) && !(obj instanceof MultipartFile)).toArray(); logger.info("请求接口 {}, method: {},ip:{}, 入参: {}", url, method, request.getRemoteAddr(), JSONObject.toJSONString(Arrays.asList(argArr))); Object result = pjp.proceed(); logger.info("接口返回 " + result); //logger.info("接口返回 " + JSONObject.toJSONString(result)); return result; } }
基于spring,aop日志切面
最新推荐文章于 2022-08-18 18:31:50 发布