尝试创建log4j2 rollingfileappender时收到错误

时间:2013-02-02 01:11:45

标签: java configuration rollingfileappender log4j2

我正在尝试使用log4j2从使用FileAppender切换到RollingFileAppender(与beta3和beta4 jar一起发生)。

我把它配置为:

<RollingFile name="RollingFile" fileName="${logdir}/${filename}" 
    filePattern="${logdir}/app-%d{yyyy-MM-dd-hh-mm-ss}_%i.log" >
    <PatternLayout>
        <pattern>%d %p %C{1.} [%t] %m%n</pattern>
    </PatternLayout>
    <Policies>
        <OnStartupTriggeringPolicy/>
    </Policies>
    <DefaultRolloverStrategy max="20"/>
</RollingFile>  
        ...
<loggers>
          <root level="ERROR">
    <appender-ref ref="RollingFile"/>
    <appender-ref ref="STDOUT"/>
</root>
        ...

在代码中,我试图以这种方式获取记录器:

Logger logger = LogManager.getLogger(this.getClass());

但是当我运行它时我得到了这个例外:

2013-02-01 17:56:54,773 ERROR Unable to invoke method createAppender in class org.apache.logging.log4j.core.appender.RollingFileAppender for element RollingFile java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.apache.logging.log4j.core.config.BaseConfiguration.createPluginObject(BaseConfiguration.java:723)
    at org.apache.logging.log4j.core.config.BaseConfiguration.createConfiguration(BaseConfiguration.java:489)
    at org.apache.logging.log4j.core.config.BaseConfiguration.createConfiguration(BaseConfiguration.java:481)
    at org.apache.logging.log4j.core.config.BaseConfiguration.doConfigure(BaseConfiguration.java:162)
    at org.apache.logging.log4j.core.config.BaseConfiguration.start(BaseConfiguration.java:120)
    at org.apache.logging.log4j.core.LoggerContext.setConfiguration(LoggerContext.java:271)
    at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:287)
    at org.apache.logging.log4j.core.LoggerContext.start(LoggerContext.java:139)
    at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:76)
    at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:31)
    at org.apache.logging.log4j.LogManager.getLogger(LogManager.java:342)
    at org.apache.logging.log4j.LogManager.getLogger(LogManager.java:301)
         ...
Caused by: java.lang.ClassCastException: org.apache.logging.log4j.core.appender.FileManager cannot be cast to org.apache.logging.log4j.core.appender.rolling.RollingFileManager
    at org.apache.logging.log4j.core.appender.rolling.RollingFileManager.getFileManager(RollingFileManager.java:73)
    at org.apache.logging.log4j.core.appender.RollingFileAppender.createAppender(RollingFileAppender.java:140)
    ... 18 more

看起来好像正在读取配置文件并且appender正在尝试创建,但不确定我做错了什么。

我尝试过切换并粘贴其他人的RollingFileAppender配置,但没有任何修改,但我仍然遇到上述错误。

感谢。

1 个答案:

答案 0 :(得分:19)

在我的案例中,配置文件包含File appender RollingFile appender

  

错误在fileName参数中 - 它在两者中都具有相同的值   追加程序。