mguymon mguymon - 1 year ago 187
Java Question

Creating an OutputstreamAppender for Logback

For my LogBack logger, I am trying to programtically create an OutputStreamAppender that writes to a ByteArrayOutputStream. Here is what I have so far:

// Destination stream
ByteArrayOutputStream stream = new ByteArrayOutputStream();

// Get LoggerContext from SLF4J
LoggerContext context = (LoggerContext)LoggerFactory.getILoggerFactory();

// Encoder
PatternLayoutEncoder encoder = new PatternLayoutEncoder();
encoder.setPattern("%d{HH:mm:ss} %-5level %logger{36} - %msg%n");

// OutputStreamAppender
OutputStreamAppender<ILoggingEvent> appender= new OutputStreamAppender<>();
appender.setName( "OutputStream Appender" );


Logger log = context.getLogger(this.getClass());
log.addAppender(appender); "text from logger");

// Output to stdout logback status

Based on everything I have read, this should be correct. Nothing from the Logger is written to the stream and the OutputStreamAppender fails to correctly initialize according to the output from

15:26:30,330 |-WARN in ch.qos.logback.core.OutputStreamAppender[OutputStream Appender] - Encoder has not been set. Cannot invoke its init method.
15:26:30,335 |-ERROR in ch.qos.logback.core.OutputStreamAppender[OutputStream Appender] - Appender [OutputStream Appender] failed to append. java.lang.NullPointerException
at java.lang.NullPointerException
at at ch.qos.logback.core.encoder.LayoutWrappingEncoder.doEncode(
at at ch.qos.logback.core.OutputStreamAppender.writeOut(
at at ch.qos.logback.core.OutputStreamAppender.subAppend(
at at ch.qos.logback.core.OutputStreamAppender.append(
at at ch.qos.logback.core.UnsynchronizedAppenderBase.doAppend(
at at ch.qos.logback.core.spi.AppenderAttachableImpl.appendLoopOnAppenders(
at at ch.qos.logback.classic.Logger.appendLoopOnAppenders(
at at ch.qos.logback.classic.Logger.callAppenders(
at at ch.qos.logback.classic.Logger.buildLoggingEventAndAppend(
at at ch.qos.logback.classic.Logger.filterAndLog_0_Or3Plus(
at at

I am stumped by the warning and NPE at
. I am setting the encoder on the Appender.

Answer Source

After poking around the source code, I found the encoder has to be set before the output stream:

OutputStreamAppender<ILoggingEvent> appender= new OutputStreamAppender<>();
appender.setName( "OutputStream Appender" );
appender.setEncoder(encoder);  // <-- must be set before outputstream
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download