Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting error trying to create log4j2 rollingfileappender

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

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.

Thanks.

like image 952
phaedrus Avatar asked Feb 02 '13 01:02

phaedrus


People also ask

What is RollingFileAppender in log4j?

RollingFileAppender extends FileAppender to backup the log files when they reach a certain size. The log4j extras companion includes alternatives which should be considered for new deployments and which are discussed in the documentation for org. apache. log4j. rolling.

What is file pattern in log4j2?

The FileNamePattern property defines the name pattern for rolled over files. In given example, it rename the rollover log files with date-month in log file name. For example, pattern {yyyy-MM-dd-HH} rollover log file every hour. The . gz file extension is used so that log4j compresses the log file automatically.

How do you get all Appenders in log4j2?

Enumeration appenders = logger. getAllAppenders(); . . . fileBackupIndex = rollingFileAppender. getMaxBackupIndex();

What is rollover strategy in log4j2?

Default Rollover Strategy. The default rollover strategy accepts both a date/time pattern and an integer from the filePattern attribute specified on the RollingFileAppender itself. If the date/time pattern is present it will be replaced with the current date and time values.


1 Answers

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

Error was in fileName parameter - it have same value in both appenders.

like image 139
Lagrang Avatar answered Oct 15 '22 12:10

Lagrang