我遇到了log4j ConsoleAppender的问题:

如果我这样初始化它:

ConsoleAppender ca = new ConsoleAppender();
ca.setLayout(new PatternLayout(PatternLayout.TTCC_CONVERSION_PATTERN));

它给出了一个错误并中断了日志记录。

错误输出:



如果我这样初始化它,则可以正常工作:
ConsoleAppender ca = new ConsoleAppender(new PatternLayout(PatternLayout.TTCC_CONVERSION_PATTERN));

有人遇到过这个问题吗?我在Bugzilla存储库中找不到它,但是如果实际上是一个问题,那将是显而易见的!

也许我是looking in the wrong place?

相关代码:
import org.apache.log4j.*;

public class ConsoleAppenderIssue {
private static Logger logger = Logger.getLogger(ConsoleAppenderIssue.class);

public static void main(String [] args) {
    ConsoleAppender ca = new ConsoleAppender();
    ca.setLayout(new PatternLayout(PatternLayout.TTCC_CONVERSION_PATTERN));

    logger.addAppender(ca);

    logger.info("log something");

}

}

最佳答案

如果您为ConsoleAppender添加look at the source,则可以看到为什么发生这种情况:

  public ConsoleAppender(Layout layout) {
    this(layout, SYSTEM_OUT);
  }

  public ConsoleAppender(Layout layout, String target) {
    setLayout(layout);
    setTarget(target);
    activateOptions();
  }

您可以看到ConsoleAppender(Layout)SYSTEM_OUT传递为目标,并且在设置布局和目标后还会调用activateOptions

如果您自己使用setLayout,那么还需要显式设置目标并调用activateOptions

10-06 09:00