本文介绍了尝试创建log4j2 rollingfileappender时收到错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我把它配置为:

<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

看起来好像正在读取配置文件而appender正试图获取已经创建但不确定我做错了什么。

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.

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

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

谢谢。

推荐答案

在我的情况下,配置文件包含文件附加器 RollingFile appender

In my case configuration file contain File appender with RollingFile appender.

这篇关于尝试创建log4j2 rollingfileappender时收到错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 04:28