pom文件引入
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
定义切面类 AspectConfig
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
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.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
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;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
@Component
@Aspect
@Order(1)
@Slf4j
public class AspectConfig {
@Pointcut("execution(* com.zan.rwj.controller..*.*(..))")
private void webLog() {
}
@Around("@within(org.springframework.web.bind.annotation.RestController)" +
"||@within(org.springframework.stereotype.Controller)")
public Object after(ProceedingJoinPoint joinPoint) throws Throwable{
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = requestAttributes.getRequest();
log.info("请求日志的打印");
log.info("请求地址:{}", Optional.ofNullable(request.getRequestURI().toString()).orElse(null));
log.info("请求方式:{}",request.getMethod());
log.info("请求类方法:{}",joinPoint.getSignature());
log.info("请求类方法参数:{}", JSONObject.toJSONString(filterArgs(joinPoint.getArgs())));
long start = System.currentTimeMillis();
Object result = joinPoint.proceed(joinPoint.getArgs());
log.info("请求响应参数{}",JSONObject.toJSONString(result));
long end = System.currentTimeMillis();
log.info("执行耗时:{}", end - start);
return result;
}
private List<Object> filterArgs(Object[] objects) {
return Arrays.stream(objects).filter(obj -> !(obj instanceof MultipartFile)
&& !(obj instanceof HttpServletResponse)
&& !(obj instanceof HttpServletRequest)).collect(Collectors.toList());
}
}
测试输出格式:
很完美,适合项目生产
标签:info,joinPoint,springboot,aop,springframework,controller,org,import,log From: https://www.cnblogs.com/rwjnb/p/17500952.html