开发者

Why is ConsoleAppender throwing "no output stream or file set for the appender named [null]"?

I'm experiencing an issue with log4j ConsoleAppender:

If I initialize it like this:

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

it gives an error and breaks the logging.

Error output:

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

If I initialize it like this it works fine:

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

Has anyone experienced this issue? I can't find it in the Bugzilla repository, but if it effectively was an issue it would be quite evident!

Perhaps I'm looking in the wrong place?

Relevant code:

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

    logg开发者_如何学编程er.info("log something");

}

}


You can see why this is happening if you look at the source for ConsoleAppender:

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

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

You can see that ConsoleAppender(Layout) passes SYSTEM_OUT as the target, and also that it calls activateOptions after setting the layout and target.

If you use setLayout yourself, then you'll also need to explicitly set the target and call activateOptions.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜