Springboot2 AOP日志配置全攻略

1 下载量 47 浏览量 更新于2024-09-03 收藏 160KB PDF 举报
"Springboot2 配置AOP日志的方法步骤" 在Spring Boot 2中,配置AOP日志是一项重要的任务,它有助于将日志记录与业务逻辑分离,提高代码的可读性和可维护性。本文将详细介绍如何在Spring Boot 2项目中设置AOP来实现日志增强。 首先,我们需要在`pom.xml`文件中添加必要的依赖。为了使用Log4j2作为日志框架,需要引入`spring-boot-starter-log4j2`依赖,并排除Spring Boot默认的日志框架,以避免冲突。同时,为了识别`log4j2.yml`配置文件,还需要引入`jackson-dataformat-yaml`依赖。以下是相关依赖的配置: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.fasterxml.jackson.dataformat</groupId> <artifactId>jackson-dataformat-yaml</artifactId> </dependency> ``` 接下来,配置`log4j2.yml`文件,定义日志输出级别、路径等,以便日志能够按需求记录到指定位置。例如: ```yaml Configuration: Appenders: Console: Name: ConsoleAppender Target: SYSTEM_OUT Layout: PatternLayout: Pattern: "%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" Loggers: Root: Level: info AppenderRef: - Ref: ConsoleAppender Logger: - Name: com.example.yourpackage Level: debug AppenderRef: - Ref: ConsoleAppender ``` 然后,创建一个AOP切面类,用于实现日志增强。例如,我们可以创建一个名为`LoggingAspect`的类,使用注解`@Aspect`声明它为一个切面,并使用`@Before`、`@After`、`@AfterThrowing`、`@Around`和`@AfterFinally`注解来定义不同的日志增强行为: ```java import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.AfterFinally; import org.aspectj.lang.annotation.Pointcut; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; @Aspect @Component public class LoggingAspect { private static final Logger log = LoggerFactory.getLogger(LoggingAspect.class); @Pointcut("execution(* com.example.yourpackage..*(..))") public void allMethods() {} @Before("allMethods()") public void beforeMethod(JoinPoint joinPoint) { log.info("Before method: {}", joinPoint.getSignature()); } @After("allMethods()") public void afterMethod(JoinPoint joinPoint) { log.info("After method: {}", joinPoint.getSignature()); } @AfterThrowing(pointcut = "allMethods()", throwing = "ex") public void afterThrowingMethod(JoinPoint joinPoint, Throwable ex) { log.error("Exception occurred in method: {} with message: {}", joinPoint.getSignature(), ex.getMessage()); } @Around("allMethods()") public Object aroundMethod(ProceedingJoinPoint joinPoint) throws Throwable { long start = System.currentTimeMillis(); try { log.info("Around before method: {}", joinPoint.getSignature()); Object result = joinPoint.proceed(); log.info("Around after method: {}, execution time: {}ms", joinPoint.getSignature(), System.currentTimeMillis() - start); return result; } catch (Throwable e) { log.error("Around exception in method: {}, execution time: {}ms", joinPoint.getSignature(), System.currentTimeMillis() - start, e); throw e; } } @AfterFinally("allMethods()") public void afterFinallyMethod(JoinPoint joinPoint) { log.info("After finally method: {}", joinPoint.getSignature()); } } ``` 在这个例子中,我们定义了一个名为`allMethods`的切点,匹配`com.example.yourpackage`包及其子包下的所有方法。然后,我们分别为方法执行前、后、异常和最终增强编写了对应的方法。 至此,我们已经完成了Spring Boot 2项目中AOP日志的配置。当运行项目时,日志将按照我们定义的规则进行记录,包括方法执行前、后、异常处理以及finally块的信息。这种方法不仅简化了日志处理,还使得日志与业务代码分离,提高了代码的可读性和可维护性。如果想要查看完整的示例项目,可以访问提供的GitHub链接:https://github.com/zhang-xiao-xiang/boot-aop。