Spring引导未加载log4j2.xml



我正试图在我的spring-boot应用程序中添加log4j2框架,并且我正在使用spring-AOP将日志记录问题与逻辑业务隔离开来。不幸的是,当我尝试记录消息时,log4j2不起作用,而是使用spring默认日志记录。

这是我尝试记录消息的Logging方面类:LoggingAspect.java

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Aspect
@Component
public class LoggingAspect {
@Around("com.obs.dqsc.api.config.AspectConfig.businessService() || com.obs.dqsc.api.config.AspectConfig.repositoryOperations()")
public Object logMethod(final ProceedingJoinPoint joinPoint)
throws Throwable {
final Class<?> targetClass = joinPoint.getTarget().getClass();
final Logger logger = LoggerFactory.getLogger(targetClass);
try {
final String className = targetClass.getSimpleName();
logger.debug(getPreMessage(joinPoint, className));
final StopWatch stopWatch = new StopWatch();
stopWatch.start();
final Object retVal = joinPoint.proceed();
stopWatch.stop();
logger.debug(getPostMessage(joinPoint, className, stopWatch.getTotalTimeMillis()));
return retVal;
} catch (final Throwable ex) {
logger.error(getErrorMessage(ex), ex);
throw ex;
}
}
private static String getPreMessage(final JoinPoint joinPoint, final String className) {
final StringBuilder builder = new StringBuilder()
.append("Entered in ").append(className).append(".")
.append(joinPoint.getSignature().getName())
.append("(");
appendTo(builder, joinPoint);
return builder
.append(")")
.toString();
}
private static String getPostMessage(final JoinPoint joinPoint, final String className, final long millis) {
return "Exit from " + className + "." +
joinPoint.getSignature().getName() +
"(..); Execution time: " +
millis +
" ms;";
}
private static String getErrorMessage(final Throwable ex) {
return ex.getMessage();
}
private static void appendTo(final StringBuilder builder, final JoinPoint joinPoint) {
final Object[] args = joinPoint.getArgs();
for (int i = 0; i < args.length; i++) {
if (i != 0) {
builder.append(", ");
}
builder.append(args[i]);
}
}
}

这是我的pom.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.obs.dqsc</groupId>
<artifactId>dqsc-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>api</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>16</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>5.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.0.0</version>
</dependency>
</dependencies>
<build>
<finalName>api-0.0.1-SNAPSHOT</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${project.parent.version}</version>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>

最后,这是我的log4j2.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{yyyy-MMM-dd HH:mm:ss} [%t] %-5level %-50c{1.} - %msg%n" />
</Console>
</Appenders>
<Loggers>
<Root level="debug">
<AppenderRef ref="Console" />
</Root>
</Loggers>
</Configuration>

而且,当我尝试使用logger.info((而不是logger.debug((时,它会在控制台中打印一些东西,但使用.debug(,它不会!(该消息显然是使用春季默认日志打印的(

我的application.properties不包含任何与log4j相关的内容,而且我的log4j2.xml在src/main/resources 中

以这种方式排除春季启动默认日志记录:

<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>

并像您已经做的那样添加spring-bootlog4j2依赖项,您就可以了。因为您只在web模块上排除了日志记录模块,它仍然会被其他依赖项拉入,例如数据mongodb模块。

最新更新