Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ByteArrayOutputStream stream = new ByteArrayOutputStream();
PrintStream printStream = new PrintStream(stream);
printStream.print("initial text");
LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
PatternLayoutEncoder encoder = new PatternLayoutEncoder();
encoder.setContext(context);
encoder.setPattern("%d{HH:mm:ss} %-5level %logger{36} - %msg%n");
encoder.start();
OutputStreamAppender<ILoggingEvent> appender= new OutputStreamAppender<>();
appender.setName( "OutputStream Appender" );
appender.setContext(context);
appender.setOutputStream(printStream);
appender.setEncoder(encoder);
appender.start();
Logger log = context.getLogger(this.getClass());
log.addAppender(appender);
log.info( "text from logger");
@mguymon

This comment has been minimized.

Copy link
Owner Author

mguymon commented Sep 27, 2013

Output from StatusPrinter.print(context):

22:28:18,707 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy] 22:28:18,708 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback-test.xml] at [file:/home/zinger/devel/projects/den/inventory_manager/target/test-classes/logback-test.xml] 22:28:18,777 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - debug attribute not set 22:28:18,779 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.ConsoleAppender] 22:28:18,783 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [STDOUT] 22:28:18,856 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [com.gameden] to DEBUG 22:28:18,856 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [com.jolbox] to DEBUG 22:28:18,857 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to INFO 22:28:18,857 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [STDOUT] to Logger[ROOT] 22:28:18,859 |-INFO in ch.qos.logback.classic.joran.JoranConfigurator@52dd1d62 - Registering current configuration as safe fallback point 22:28:18,861 |-WARN in ch.qos.logback.core.OutputStreamAppender[OutputStream Appender] - Encoder has not been set. Cannot invoke its init method. 22:28:18,867 |-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(LayoutWrappingEncoder.java:116) at at ch.qos.logback.core.OutputStreamAppender.writeOut(OutputStreamAppender.java:193) at at ch.qos.logback.core.OutputStreamAppender.subAppend(OutputStreamAppender.java:217) at at ch.qos.logback.core.OutputStreamAppender.append(OutputStreamAppender.java:108) at at ch.qos.logback.core.UnsynchronizedAppenderBase.doAppend(UnsynchronizedAppenderBase.java:88)

@tony19

This comment has been minimized.

Copy link

tony19 commented Nov 9, 2013

I tried your example snippet above, but I'm not seeing any exceptions. The status output you've shown above is actually from file:/home/zinger/devel/projects/den/inventory_manager/target/test-classes/logback-test.xml and not from the Java snippet. The warning message right before the NPE is worth investigating:

22:28:18,861 |-WARN in ch.qos.logback.core.OutputStreamAppender[OutputStream Appender] - Encoder has not been set. Cannot invoke its init method.
22:28:18,867 |-ERROR in ch.qos.logback.core.OutputStreamAppender[OutputStream Appender] - Appender [OutputStream Appender] failed to append. java.lang.NullPointerException
at java.lang.NullPointerException
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.