I use annotations to implement the operation log of the interface

In the project, we will need to obtain the operation log of the interface. For example, get the interface name of the interface, the operator, the running time of the interface, the service to which it belongs, the type of the interface (addition, deletion, modification, check), and so on. The basic approach is to store these operation records in the library after the interface method is executed. This code is written in the interface, but this violates the single responsibility principle in the design principle. A common practice is to use AOP to dynamically insert logging code at runtime. Here I do it with annotations.

Create logging table

First, let's create a log record table:

operation table

When then write the entity class:

Write log notes

We first define an annotationOperationLog

Several parameters are defined in this annotation:

opType: business type

opBusinessName: business name, such as membership service, order service, etc.

opBusinessId: Business source ID, generally the primary key ID

Define business type classesOperationType

This business type class defines the type of interface: add, delete, modify, and check.

Next we write the aspect classOperationLogAspect

Get the annotations above the method:

Method method = ((MethodSignature) pjp.getSignature()).getMethod();
//获取目标方法上面的注解
OperationLog opLog = method.getAnnotation(OperationLog.class);

Implementation target method:

try {
    // 执行目标方法
    result = pjp.proceed();
} catch (Throwable throwable) {
    throw new Exception(throwable);
}

Record the execution time before and after the execution of the target method:

TimeInterval timer = DateUtil.timer();
...
 // 执行目标方法
...
long executeTime = timer.intervalRestart(); 

Parsing the expression on the annotation is to get the business id:

// 解析表达式,获取结果
String itemId = String.valueOf(expression.getValue(context));

Encapsulate the data in an object:

Operation operation = Operation.builder()
                    //.id(snowflake.nextId())
                    .opType(opLog.opType().name())
                    .opBusinessName(opLog.opBusinessName())
                    .opBusinessId(itemId)
                    .opTime(String.valueOf(executeTime))
                    //这里可以添加操作人
                    .build();

Record the log and store it in the warehouse:

private void handle(Operation operation) {
    // 通过日志打印输出
    log.info("opType = " + operation.getOpType() + ",opItem = " + operation.getOpBusinessName() + ",opItemId = " + operation.getOpBusinessId() + ",opTime = " + operation.getOpTime());
    // 持久化入库
    operationService.save(operation);
}

The overall code is as follows:

Test notes

The test annotation code is as follows:

@RequestMapping("/getById")
@ResponseBody
@OperationLog(opType = OperationType.QUERY,opBusinessName = "会员服务",opBusinessId = "#id")
public Member getById(@RequestParam String id) {
    return memberService.getById(id);
}

We getByIdadded a note @OperationLogto this interface, indicating that we want to record the operation record of this interface. opType is the query type, the business to which it belongs is membership service, and the business id is the parameter id of the interface.

Start code running interface:

The console printed the log:

And the database also has records:

This is the end of the code. Of course, we can also add information such as the path of the interface name and the call chain of the interface in the log record table.

Recommended in the past

Scan the QR code to get more exciting. Or search Lvshen_9 on WeChat , you can reply to get information in the background

  1. 回复"java" 获取java电子书;

  2. 回复"python"获取python电子书;

  3. 回复"算法"获取算法电子书;

  4. 回复"大数据"获取大数据电子书;

  5. 回复"spring"获取SpringBoot的学习视频。

  6. 回复"面试"获取一线大厂面试资料

  7. 回复"进阶之路"获取Java进阶之路的思维导图

  8. 回复"手册"获取阿里巴巴Java开发手册(嵩山终极版)

  9. 回复"总结"获取Java后端面试经验总结PDF版

  10. 回复"Redis"获取Redis命令手册,和Redis专项面试习题(PDF)

  11. 回复"并发导图"获取Java并发编程思维导图(xmind终极版)

Another: Click [ My Benefits ] to have more surprises.

Guess you like

Origin blog.csdn.net/wujialv/article/details/109734732