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

1

14

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

}

}
Izak answered 30/5, 2011 at 10:13 Comment(0)
G
22

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.

Glutathione answered 30/5, 2011 at 10:18 Comment(2)
Great! You help me so much. I haven't found it because I debugged the log method to find out where the exception arises. But it is quite deep and when found, it not helps in getting the solution. Looked inside ConsoleAppender constructor and understood. thanks!Izak
@Glutathione can you help with my question https://mcmap.net/q/681968/-secure-encrypt-log4j-filesLatvina

© 2022 - 2024 — McMap. All rights reserved.