gpt4 book ai didi

java - Log4j2,文件中没有日志

转载 作者:行者123 更新时间:2023-11-30 07:07:21 35 4
gpt4 key购买 nike

我试图在没有标准结构的 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

现在可以了。

关于java - Log4j2,文件中没有日志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39851418/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com