java 尝试创建 log4j2 滚动文件附加程序时出错

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14657572/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 16:59:44  来源:igfitidea点击:

getting error trying to create log4j2 rollingfileappender

javaconfigurationrollingfileappenderlog4j2

提问by phaedrus

I'm trying to switch from using FileAppender to the RollingFileAppender using log4j2 (happens both with beta3 and beta4 jars).

我正在尝试从使用 FileAppender 切换到使用 log4j2 的 RollingFileAppender(同时发生在 beta3 和 beta4 jar 中)。

I have it configured as:

我将它配置为:

<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>
        ...

and in the code, I'm trying to get the logger this way:

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

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

but I'm getting this exception when I run it:

但是我在运行它时遇到了这个异常:

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

It looks as if the config file is being read and the appender is trying to get created but not sure what I'm doing wrong.

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

I've tried cutting and pasting other people's RollingFileAppender configuration w/o any modification but I still get the above error.

我试过在没有任何修改的情况下剪切和粘贴其他人的 RollingFileAppender 配置,但我仍然收到上述错误。

Thanks.

谢谢。

回答by Lagrang

In my case configuration file contain File appenderwith RollingFile appender.

在我的情况下配置文件中包含File appenderRollingFile appender

Error was in fileNameparameter - it have same value in both appenders.

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