2017-03-31 74 views
0

Log4j2在我的RollingFile Appender的filePattern中讀取日期時遇到問題。當我加入TimeBasedTriggeringPolicy然後將其重新配置我的配置文件,我得到這個錯誤:Log4j2-RollingFile當TimeBasedTriggeringPolicy啓用時不寫入

「錯誤無法注入到領域的建設者類插件類型的類org.apache.logging.log4j.core.appender.RollingFileAppender,元素RollingFile java.lang.IllegalStateException:模式不包含日期「

我已經在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) 

回答

1

錯誤消息「模式不包含日期」顯然是錯誤的,因爲您的模式是filePattern="${baseDir}/cim-%d{yyyy-MM-dd}.log",其中確實包含日期

但是,模式存在問題。您確實指定了兩個觸發策略,一個基於日期和一個基於大小的策略。

SizeBasedTriggeringPolicy也需要在filePattern,具體%i東西,因爲當這種政策觸發,它需要的文件重命名爲東西

以您平常在這樣一堆翻了個身日誌文件的最終基於尺寸的政策:

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

的filePattern需要包含%i轉換模式,告訴Log4j2哪裏放櫃檯,否則將無法重命名該文件。

+0

感謝您的幫助。我原來在log4j2.xml中有%i,但仍然無法正常工作。我剛剛重新配置了很多次,以解決我發佈時忘記重新加入的問題。我仍然不知道問題是什麼,但是我在某個時候丟失了與我的數據庫的互聯網連接,並且必須重新啓動,當然一切都開始正常工作,哈哈。再次感謝。 – iBeatzPrograms

相關問題