JavaLib | use AOP to help you log

Posted by ryanlwh on Sat, 04 Jan 2020 11:04:36 +0100

This time, let's go straight to the point.

First step

Using this module, you need to add package dependency:

The second step

You need to tell me your pointcut (inherit LBaseWebLogAspect, implement pointCut() method, and configure your pointcut)

@Aspect
@Component
class WebLogAspect extends LBaseWebLogAspect {

    @Pointcut("execution(* com.fengwenyi.javalibexampleaoplog..*.*(..))")
    @Override
    protected void pointCut() { }
}

The third step

Write an interface for testing, let's use the last incision (complete code)

package com.fengwenyi.javalibexampleaoplog;

import com.fengwenyi.javalib.aop.LBaseWebLogAspect;
import com.fengwenyi.javalib.result.DefaultReturnCode;
import com.fengwenyi.javalib.result.Result;
import com.fengwenyi.javalib.result.ResultResponseUtil;
import lombok.Data;
import lombok.experimental.Accessors;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class JavalibExampleAoplogApplication {

    public static void main(String[] args) {
        SpringApplication.run(JavalibExampleAoplogApplication.class, args);
    }

    /**
     * Get all user data
     * @return (json)
     */
    @GetMapping("/getUsers")
    public Result getUsers() {

        User user1 = new User().setName("Feng Wen Yi").setAge(26).setGender("male");
        User user2 = new User().setName("Zhang San").setAge(25).setGender("female");
        User user3 = new User().setName("Li Si").setAge(23).setGender("secrecy");
        User user4 = new User().setName("Wang Wu").setAge(20).setGender("Unknown");

        User [] users = {user1, user2, user3, user4};

        return ResultResponseUtil.ok().status(DefaultReturnCode.SUCCESS).data(users);

    }
}

@Aspect
@Component
class WebLogAspect extends LBaseWebLogAspect {

    @Pointcut("execution(* com.fengwenyi.javalibexampleaoplog..*.*(..))")
    @Override
    protected void pointCut() { }
}

@Data
@Accessors(chain = true)
class User {

    // Full name
    private String name;
    // Age
    private Integer age;
    // Gender
    private String gender;

}

The fourth step

Let's zoom in and watch it carefully

The log mainly records the following information:

  • Request client IP

  • Request URL

  • How to request (GET/POST /...)

  • Requested classes and corresponding methods

  • Requested parameter (json format string)

  • Data object returned

  • Time spent on this request (unit: NS; NS nanosecond 1s=1000ms 1ms=1000us 1us=1000ns)

data

[1] How does the Spring Boot project log? A kind of

[2] Test code in this section

[3] JavaLib ☜

Topics: Lombok JSON Spring