[英]Log4j2, no logs in file
我试图在没有标准结构的 Maven 项目中配置 log4j2,但即使创建了文件,也没有附加任何内容。
我在 pom.xml 中有以下依赖项
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j-version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>${log4j-version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>${log4j-version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j-version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson-version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson-version}</version>
</dependency>
我正在使用以下版本
<properties>
<slf4j-version>1.7.21</slf4j-version>
<log4j-version>2.6.2</log4j-version>
<jackson-version>2.8.2</jackson-version>
</properties>
和以下 log4j2.properties
status = info
rootLogger.level = info
name = PropertiesConfig
property.fileName = ./log/log.txt
appender.rolling.type = RollingFile
appender.rolling.name = RollingFile
appender.rolling.fileName = ${fileName}
appender.rolling.filePattern = ./log/log_%d{dd-MM-yy}.txt
appender.rolling.append = true
appender.rolling.layout.type = JSONLayout
appender.rolling.layout.complete = true
appender.rolling.policies.type = Policies
appender.rolling.policies.time.type = TimeBasedTriggeringPolicy
appender.rolling.policies.time.interval = 1
appender.rolling.policies.time.modulate = true
appender.rolling.strategy.type = DefaultRolloverStrategy
appender.rolling.strategy.max = 5
另外,由于项目的结构,我正在像这样设置属性文件
LoggerContext context = (LoggerContext) LogManager.getContext(false);
File file = new File(CONFIGURATION_DIR + File.separator + "log4j2.properties");
// this will force a reconfiguration
context.setConfigLocation(file.toURI());
即使文件被创建,文件也没有附加任何内容,我也没有错误。 我是不是错过了什么,或者做错了什么?
看起来我缺少 log4j2.properties 文件的以下配置:
logger.rolling.name = name
logger.rolling.level = debug
logger.rolling.additivity = false
logger.rolling.appenderRef.rolling.ref = RollingFile
现在它起作用了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.