gpt4 book ai didi

java - 为什么 ConsoleAppender 抛出 "no output stream or file set for the appender named [null]"?

转载 作者:搜寻专家 更新时间:2023-10-30 21:06:04 26 4
gpt4 key购买 nike

我在使用 log4j ConsoleAppender 时遇到问题:

如果我这样初始化它:

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

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

错误输出:

log4j:ERROR No output stream or file set for the appender named [null].

如果我这样初始化它,它工作正常:

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");

}

}

最佳答案

如果您 look at the source 就会明白为什么会发生这种情况对于 ConsoleAppender:

  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

关于java - 为什么 ConsoleAppender 抛出 "no output stream or file set for the appender named [null]"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6174973/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com