尝试在log4j中实例化FileAppender时出错

时间:2016-08-19 14:22:09

标签: java apache logging log4j

这是代码:

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

第二行收到以下错误:

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

然而,log4j javadocs表明一个构造函数是一个有效的构造函数,其中PatternLayout作为第一个参数,String作为第二个参数(对于文件名)。

1 个答案:

答案 0 :(得分:2)

您的班级导入错误。它应该是import org.apache.log4j.FileAppender;,而不是import org.apache.logging.log4j.core.appender.FileAppender;或类似。