package com.example.springbootaoplog.config;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
/**
* aop日志打印配置
*
* @author hongcunlin
*/
@Slf4j
@Aspect
@Component
public class AopLogConfig {
/**
* 切点路径:Controller层的所有方法
*/
@Pointcut ( "execution(public * com.example.springbootaoplog.controller.*.*(..))" )
public void methodPath() {
}
/**
* 入参
*
* @param joinPoint 切点
*/
@Before (value = "methodPath()" )
public void before(JoinPoint joinPoint) {
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
String url = requestAttributes.getRequest().getRequestURL().toString();
log.info( "请求 = {}, 入参 = {}" , url, JSON.toJSONString(joinPoint.getArgs()));
}
/**
* 出参
*
* @param res 返回
*/
@AfterReturning (returning = "res" , pointcut = "methodPath()" )
public void after(Object res) {
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
String url = requestAttributes.getRequest().getRequestURL().toString();
log.info( "请求 = {}, 入参 = {}" , url, JSON.toJSONString(res));
}
}
|