Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

    logger.info("log something");

}

}
like image 613
AgostinoX Avatar asked May 30 '11 10:05

AgostinoX


1 Answers

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.

like image 53
skaffman Avatar answered Sep 26 '22 23:09

skaffman