yitzih yitzih - 1 year ago 84
Java Question

Getting error when trying to instantiate FileAppender in log4j

This is the code:

PatternLayout patternLayout = new PatternLayout("%d{MM-dd-yyyy H:mm:ss.SSS} [%p] (%C{1}.%M) %n %m");
FileAppender fileAppender = new FileAppender(patternLayout, fileName);

The 2nd line is getting the following error:

Error:(40, 37) java: constructor FileAppender in class org.apache.logging.log4j.core.appender.FileAppender cannot be applied to given types;
required: java.lang.String,org.apache.logging.log4j.core.Layout<? extends java.io.Serializable>,org.apache.logging.log4j.core.Filter,org.apache.logging.log4j.core.appender.FileManager,java.lang.String,boolean,boolean,org.apache.logging.log4j.core.net.Advertiser
found: org.apache.log4j.PatternLayout,java.lang.String
reason: actual and formal argument lists differ in length

However the log4j javadocs show that a constructor with a PatternLayout as its 1st parameter and a String as the 2nd parameter (for the file name) is a valid constructor.

Answer Source

You have a wrong import in your class. It should be import org.apache.log4j.FileAppender;, not import org.apache.logging.log4j.core.appender.FileAppender; or similar.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download