gpt4 book ai didi

java - 启用 TimeBasedTriggeringPolicy 时 Log4j2-RollingFile 不写入

转载 作者:行者123 更新时间:2023-11-29 04:32:44 30 4
gpt4 key购买 nike

Log4j2 在读取我的 RollingFile Appender 的 filePattern 中的日期时遇到问题。当我添加 TimeBasedTriggeringPolicy 并重新配置我的配置文件时,出现此错误:

"ERROR Unable to inject fields into builder class for plugin type class org.apache.logging.log4j.core.appender.RollingFileAppender, element RollingFile. java.lang.IllegalStateException: Pattern does not contain a date"

我在 log4j2 代码中放置了一个断点,发现原因是作为 PatternProcessor 对象一部分的频率变量为空。我不知道为什么这是 null 或如何将它设置为 null 以外的值。

如果我注释掉 TimeBasedTriggeringPolicy,appender 开始工作并将日志写入我的文件。

这是我的 POM 中的 log4j 依赖项:

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-web</artifactId>
<version>2.6.2</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>1.7.7</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.7</version>
</dependency>

这是我的 log4j2.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration monitorInterval="30" status="DEBUG">
<Properties>
<Property name="baseDir">/logs/oracle/domains/cim</Property>
</Properties>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%highlight{%d{DEFAULT} [%t] %-5p %c -%m%n}"/>
</Console>
<RollingFile name="RollingFile" fileName="${baseDir}/cim.log"
filePattern="${baseDir}/cim-%d{yyyy-MM-dd}.log" append="true">
<PatternLayout pattern="%d{DEFAULT} %-5p %c - %m%n"/>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
<SizeBasedTriggeringPolicy size="10 MB" />
</Policies>
<DefaultRolloverStrategy max="31"/>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="com.cim" level="debug" additivity="true">
<Appender-Ref ref="RollingFile"/>
</Logger>
<Root level="debug" additivity="false">
<Appender-Ref ref="Console"/>
</Root>
</Loggers>
</Configuration>

这是上述错误消息之后的初始堆栈跟踪:

at org.apache.logging.log4j.core.appender.rolling.PatternProcessor.getNextTime(PatternProcessor.java:119)
at org.apache.logging.log4j.core.appender.rolling.TimeBasedTriggeringPolicy.initialize(TimeBasedTriggeringPolicy.java:59)
at org.apache.logging.log4j.core.appender.rolling.CompositeTriggeringPolicy.initialize(CompositeTriggeringPolicy.java:52)
at org.apache.logging.log4j.core.appender.rolling.RollingFileManager.setTriggeringPolicy(RollingFileManager.java:216)
at org.apache.logging.log4j.core.appender.rolling.RollingFileManager.updateData(RollingFileManager.java:439)
at org.apache.logging.log4j.core.appender.AbstractManager.getManager(AbstractManager.java:119)
at org.apache.logging.log4j.core.appender.OutputStreamManager.getManager(OutputStreamManager.java:114)
at org.apache.logging.log4j.core.appender.rolling.RollingFileManager.getFileManager(RollingFileManager.java:128)
at org.apache.logging.log4j.core.appender.RollingFileAppender$Builder.build(RollingFileAppender.java:135)
at org.apache.logging.log4j.core.appender.RollingFileAppender$Builder.build(RollingFileAppender.java:58)
at org.apache.logging.log4j.core.config.plugins.util.PluginBuilder.build(PluginBuilder.java:122)
at org.apache.logging.log4j.core.config.AbstractConfiguration.createPluginObject(AbstractConfiguration.java:942)
at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:882)
at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:874)
at org.apache.logging.log4j.core.config.AbstractConfiguration.doConfigure(AbstractConfiguration.java:498)
at org.apache.logging.log4j.core.config.AbstractConfiguration.initialize(AbstractConfiguration.java:227)
at org.apache.logging.log4j.core.config.AbstractConfiguration.start(AbstractConfiguration.java:239)
at org.apache.logging.log4j.core.LoggerContext.setConfiguration(LoggerContext.java:530)
at org.apache.logging.log4j.core.LoggerContext.onChange(LoggerContext.java:653)
at org.apache.logging.log4j.core.config.ConfiguratonFileWatcher$ReconfigurationRunnable.run(ConfiguratonFileWatcher.java:65)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)

最佳答案

错误信息“模式不包含日期”显然是错误的,因为你的模式是filePattern="${baseDir}/cim-%d{yyyy-MM-dd}.log",其中确实包含日期

但是,模式有问题。您确实指定了两个触发策略,一个是基于日期的,一个是基于大小的。

SizeBasedTriggeringPolicy 还需要 filePattern 中的一些东西,特别是 %i,因为当这个策略触发时,它需要将文件重命名为 something

使用基于大小的策略,您通常会得到一堆滚动的日志文件,如下所示:

app-1.log
app-2.log
app-3.log
...

filePattern 需要包含一个%i 转换模式,以告诉 Log4j2 将计数器放在哪里,否则它将无法重命名文件。

关于java - 启用 TimeBasedTriggeringPolicy 时 Log4j2-RollingFile 不写入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43147821/

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