SpringBook Basic Tutorial 2-1-6 Log Specification - Unified Processing of Web Logs Using AOP

Keywords: Java JSON Spring github

1. overview

As the entrance of the service, the Web layer is essential to log the request parameters and response results. This paper unifies the processing of the Web log with AOP aspect technology.

2. General Web Logging Method

Most people print their logs directly in Controller, as follows:

@Slf4j
@RestController
public class UserController {
    @PostMapping("/user")
    public R addUser(@RequestBody User user){
        log.info("user: {}", JSON.toJSONString(user));
        R r = R.isOk().data(user);
        log.info("result: {}", JSON.toJSONString(user));
        return r;
    }
}

This way of writing leads to duplication of log code and coupling in the Controller layer, which is neither elegant nor lazy enough.

  • JSON class is a tool class of fastjson, recommended for use
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.39</version>
</dependency>

3. Graceful recording of web logs

  • pom file, introducing AOP dependencies
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
</dependency>
  • Simply create Controller
@RestController
@RequestMapping("/log")
public class AopLogController {

    @PostMapping("/user")
    public R addUser(@RequestBody User user){

        return R.isOk().data(user);
    }
}
  • Create log facets
@Slf4j
@Aspect
@Component
public class WebLogAspect {
    @Pointcut("execution(public * com.mkeeper.controller.logging..*.*(..))")
    public void webLog(){}

    @Before("webLog()")
    public void doBefore(JoinPoint joinPoint){
        // Receive the request and record the content of the request
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        // Record the content of the request
        log.info("<<<<<<<<<<<<<<<<<<<<<<<<");
        log.info("URL : " + request.getRequestURL().toString());
        log.info("HTTP_METHOD : " + request.getMethod());
        log.info("IP : " + request.getRemoteAddr());
        log.info("CLASS_METHOD : " + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
        log.info("ARGS : " + Arrays.toString(joinPoint.getArgs()));

    }

    @AfterReturning(returning = "ret", pointcut = "webLog()")
    public void doAfterReturning(Object ret){
        // Processing the request and returning the content
        log.info("RESPONSE : " + ret);
        log.info(">>>>>>>>>>>>>>>>>>>>>>>>>");
    }
}

Aspect defines a java class as a tangent class Pointcut defines a pointcut, which can be a regular expression, such as all functions under a package in the following example, or an annotation, etc. Before cuts in at the beginning of the entry point After cuts in at the end of the entry point AfterReturning cuts in after the pointcut return content (which can be used to do some processing on processing return values) Around cuts into content before and after the entry point and controls when to execute the entry point's own content AfterThrowing is used to handle the processing logic after an exception is thrown in the content entry section

  • Other basic classes can be seen Source code
  • Note: If you need CGLIB to implement AOP, you need to configure spring.aop.proxy-target-class=true, otherwise the default is the standard Java implementation.

4. Test results

! [Directory] (/ SpringBook Basic Tutorial 2-1-6 Log Specification - Unified Processing of Web Logs with AOP/1.png)

5. Engineering catalogue

! [Directory] (/ SpringBook Basic Tutorial 2-1-6 Log Specification - Unified Processing of Web Logs / Directories. PNG with AOP)

6. concluding remarks

What to say? Any suggestions. Welcome to leave a message for discussion. Source of this article.

Welcome to pay attention to the blogger's public number and push the latest articles as soon as possible.

Posted by the_crazyman on Fri, 11 Jan 2019 22:33:11 -0800