springboot do use AOP access request log

springboot do use AOP access request log: The introduction of aop and log springboot

1, pom.xml introduced:

<!--springBoot的aop,已经集成了spring aop和AspectJ-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-logging -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </dependency>

 

2, the aspect class configuration:

@Component
@Aspect
public class LogAspect {

    private static final Logger logger = LoggerFactory.getLogger(LogAspect.class);
    //切入点表达式,com.springboot.controller自己controller包的路径
    @Pointcut("execution(public * com.springboot.controller..*.*(..))")
    public void pointCut(){

    }

    @Before("pointCut()")
    public void beforeMethod(JoinPoint joinPoint){
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();

        Request the HttpServletRequest = servletRequestAttributes.getRequest (); 
      // Get the parameter information to be printed String requestURI
= Request.getRequestURI (); String Method request.getMethod = (); String remoteAddr request.getRemoteAddr = ();
     // used here is Ali FASTJSON String JSONString =
JSON.toJSONString (joinPoint.getArgs ());
      // print information logger.info (
"------------------------ request information ---------------------------------- " ); logger.info ( " request time: {} " , new new the SimpleDateFormat. ( "the mM-dd-YYYY HH: mm: SS") the format ( new new a Date ())); logger.info ("remoteAddr: {} ",remoteAddr); logger.info("requestURI : {}",requestURI); logger.info("Controller : {}", joinPoint.getTarget().getClass()); logger.info("method type: {}" ,method); logger.info("req paras: {}",jsonString); logger.info("------------------------请求信息-----------------------------------"); } }

effect:

 com.springboot.common.aop.LogAspect      : ------------------------请求信息----------------------------------
 com.springboot.common.aop.LogAspect      : 请求时间 :2020-01-02 22:38:40
 com.springboot.common.aop.LogAspect      : remoteAddr: 0:0:0:0:0:0:0:1 
 com.springboot.common.aop.LogAspect      : requestURI : /user/10001
 com.springboot.common.aop.LogAspect      : Controller : class com.springboot.controller.UserController
 com.springboot.common.aop.LogAspect      : method type: GET
 com.springboot.common.aop.LogAspect      : req paras: [10001]
 com.springboot.common.aop.LogAspect      : ------------------------请求信息---------------------------------

Here only the access request log printing, may also be placed after the notification, the print information in response to, in conjunction with other operations can be printed around the notification time of program execution.

。。。

Guess you like

Origin www.cnblogs.com/tdyang/p/12142177.html