java - 无法使用 log4j ver 2.13.3 或更高版本中的 log4j2.properties 将日志写入文件
问题描述
我正在尝试在 Spring 项目中使用 log4j2。我的以下配置适用于 log4j2 版本 2.6.1,但是当我将版本更改为 2.13.3 或 2.14.1(最新版本)时,所有日志都会打印到控制台而不是日志文件。你知道为什么会这样吗?
这是我的 log4j2.properties 配置
status = error
name = PropertiesConfig
property.filename = C:\\logs\\debug.log
filters = threshold
filter.threshold.type = ThresholdFilter
filter.threshold.level = debug
appenders = rolling
appender.rolling.type = RollingFile
appender.rolling.name = RollingFile
appender.rolling.fileName = ${filename}
appender.rolling.filePattern = C:\\logs\\Previous\\debug-backup-%d{MM-dd-yy-HH-mm-ss}-%i.log.gz
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = %d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %msg%n
appender.rolling.policies.type = Policies
appender.rolling.policies.time.type = TimeBasedTriggeringPolicy
appender.rolling.policies.time.interval = 100000
appender.rolling.policies.time.modulate = true
appender.rolling.policies.size.type = SizeBasedTriggeringPolicy
appender.rolling.policies.size.size=10MB
appender.rolling.strategy.type = DefaultRolloverStrategy
appender.rolling.strategy.max = 20
loggers = rolling
logger.rolling.name = com.example.demo
logger.rolling.level = debug
logger.rolling.additivity = false
logger.rolling.appenderRef.rolling.ref = RollingFile
我的申请文件
package com.example.demo;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringLog4j2Application {
private static final Logger LOGGER = LogManager.getLogger(SpringLog4j2Application.class);
public static void main(String[] args) {
SpringApplication.run(SpringLog4j2Application.class, args);
System.out.println("Hello log4j2");
LOGGER.debug("This is debug statement");
LOGGER.info("This is info log");
LOGGER.warn("This is warn log");
LOGGER.error("This is error log", new NullPointerException());
LOGGER.fatal("This is a fatal log");
LOGGER.trace("This is trace log");
}
}
还有我的 pom.xml
...
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.6</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>Log4J2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-log4j2</name>
<description>Demo Spring Boot project for Log4j2</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</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.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.13.3</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.13.3</version>
</dependency>
</dependencies>
...
</project>
解决方案
哦,看来我们必须排除一些新的 jar,以及更新的 log4j 版本。我们可以通过将这些行添加到我们的 pom.xml 来做到这一点
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
所以我的新 pom.xml 看起来像这样
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<!-- HERE ARE THE NEW LINES -->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</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.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.13.3</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.13.3</version>
</dependency>
</dependencies>
推荐阅读
- ios - 我在 SwiftUI 中找到了两种管理视图的方法,但我不知道有什么区别,也不知道哪种方法更好
- r - 具有泊松分布的对数线性模型的偏移项
- python - 关于优化一些嵌套for循环的非常noob Python问题
- python - 网页抓取时如何切换框?
- ruby-on-rails - 我在使用 Rails N + 1 查询时遇到问题。不知道如何包含活动存储以避免 N + 1 查询
- javascript - 将返回的变量从函数中取出并放入 document.write
- batch-file - 根据文件名的开头创建目录
- django - Django DRF:看不到登录选项
- c++ - 为什么使用动态 ptr 数组检测到分段错误或双重释放?
- python - 如何在不使用 int() 的情况下将字符串转换为整数