Log4j2在我的RollingFile Appender的filePattern中读取日期时遇到问题。当我添加TimeBasedTriggeringPolicy并重新配置我的配置文件时,我收到此错误:
“错误无法将字段注入构建器类的插件类型类org.apache.logging.log4j.core.appender.RollingFileAppender,元素RollingFile.java.lang.IllegalStateException:Pattern不包含日期”
我在log4j2代码中放了一个断点,发现原因是PatternProcessor对象的一部分频率变量为null。我不知道为什么这是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)
答案 0 :(得分:1)
错误消息“模式不包含日期”显然是错误的,因为您的模式为filePattern="${baseDir}/cim-%d{yyyy-MM-dd}.log"
,其中 包含日期
但是,模式存在问题。您确实指定了两个触发策略,基于日期和基于大小的策略。
SizeBasedTriggeringPolicy
还需要filePattern中的某些内容,特别是%i
,因为当此策略触发时,需要将文件重命名为某些内容。
使用基于大小的策略,您通常会得到一堆翻转日志文件,如下所示:
app-1.log
app-2.log
app-3.log
...
filePattern需要包含%i
转换模式,告诉Log4j2放置计数器的位置,否则它将无法重命名文件。