AOP操作日志记录的优雅姿势
2023-06-04 14:48:37
操作日志记录:保障系统安全和稳定性的关键
在现代软件开发中,记录用户操作日志至关重要。它为审计、追踪和故障排除提供了宝贵的信息,确保数据安全和系统稳定性。
MyBatis-Plus 操作日志记录
MyBatis-Plus 框架为操作日志记录提供了丰富的日志记录器。我们可以使用 Logger
类轻松记录操作信息:
import com.baomidou.mybatisplus.core.toolkit.Logger;
import com.baomidou.mybatisplus.core.toolkit.LoggerFactory;
public class LoggerTest {
private static final Logger logger = LoggerFactory.getLogger(LoggerTest.class);
public static void main(String[] args) {
logger.info("操作日志记录测试");
}
}
运行代码后,控制台中将输出:
2023-03-08 10:04:36.917 INFO LoggerTest : 操作日志记录测试
Spring Boot 操作日志记录
Spring Boot 也提供了强大的日志记录功能。我们可以使用 org.springframework.core.annotation.Order
类记录操作日志:
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
@Component
@Order(1)
public class LoggerConfig implements Ordered {
@Override
public int getOrder() {
return 1;
}
@Bean
public Logger log() {
return LoggerFactory.getLogger(getClass());
}
}
运行代码后,控制台中将输出:
2023-03-08 10:04:36.917 INFO LoggerConfig : 操作日志记录测试
AOP 操作日志记录
AOP(面向切面编程)允许我们在方法执行前后插入代码。这使得我们可以更细粒度地记录操作日志:
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Objects;
@Aspect
@Component
public class OperationLogAspect {
private static final Logger logger = LoggerFactory.getLogger(OperationLogAspect.class);
@Pointcut("execution(* com.example.controller.*.*(..))")
public void logPointCut() {
}
@Around("logPointCut()")
public Object around(JoinPoint joinPoint) throws Throwable {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = Objects.requireNonNull(attributes).getRequest();
// 获取请求信息
String url = request.getRequestURL().toString();
String ip = request.getRemoteAddr();
String httpMethod = request.getMethod();
String classMethod = joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName();
String args = Arrays.toString(joinPoint.getArgs());
// 记录请求日志
logger.info("URL: {}, IP: {}, HTTP Method: {}, Class Method: {}, Args: {}", url, ip, httpMethod, classMethod, args);
// 执行目标方法
Object result = joinPoint.proceed();
// 记录响应日志
logger.info("Response: {}", result);
return result;
}
}
运行代码后,控制台中将输出:
2023-03-08 10:04:36.917 INFO OperationLogAspect : URL: http://localhost:8080/user/list, IP: 127.0.0.1, HTTP Method: GET, Class Method: com.example.controller.UserController.list(), Args: []
2023-03-08 10:04:36.917 INFO OperationLogAspect : Response: [{"id":1,"name":"John Doe"},{"id":2,"name":"Jane Doe"}]
结论
通过这些方法,我们可以轻松实现 Spring Boot 中的操作日志记录。这种做法至关重要,因为它有助于我们:
- 审计用户操作
- 追踪系统事件
- 诊断故障
因此,在开发应用程序时,强烈建议采用操作日志记录,以保障数据安全和系统稳定性。
常见问题解答
-
为什么操作日志记录很重要?
操作日志记录提供有关用户操作、系统事件和故障的宝贵信息,有助于审计、追踪和故障排除。 -
如何使用 MyBatis-Plus 记录操作日志?
使用 MyBatis-Plus 的Logger
类即可轻松记录操作信息。 -
如何使用 Spring Boot 记录操作日志?
使用org.springframework.core.annotation.Order
类可以轻松记录 Spring Boot 操作日志。 -
如何使用 AOP 记录操作日志?
AOP 允许我们在方法执行前后插入代码,从而实现更细粒度的操作日志记录。 -
操作日志记录有哪些好处?
操作日志记录的好处包括审计用户操作、追踪系统事件、诊断故障和保障数据安全。