Created
January 2, 2013 18:13
-
-
Save dfilimon/4436581 to your computer and use it in GitHub Desktop.
Logback output to stream.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ByteArrayOutputStream bs = new ByteArrayOutputStream(2048); | |
LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory(); | |
lc.reset(); | |
lc.setName("context"); | |
lc.start(); | |
PatternLayoutEncoder encoder = new PatternLayoutEncoder(); | |
encoder.setContext(lc); | |
encoder.setPattern("%message\n"); | |
encoder.setImmediateFlush(true); | |
encoder.setCharset(Charset.defaultCharset()); | |
encoder.start(); | |
OutputStreamAppender<ILoggingEvent> appender = new OutputStreamAppender<ILoggingEvent>(); | |
appender.setContext(lc); | |
appender.setOutputStream(bs); | |
appender.setName("appender"); | |
appender.setEncoder(encoder); | |
appender.start(); | |
Logger l = lc.getLogger("test"); | |
l.addAppender(appender); | |
l.setLevel(Level.DEBUG); | |
assertThat(lc.isStarted(), is(true)); | |
assertThat(encoder.isStarted(), is(true)); | |
assertThat(appender.isStarted(), is(true)); | |
l.debug("Hello!"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, i had a similar problem and i found your solution. For me it didn't work either (like you said on the mailing list). So i used StatusPrinter.print(lc); to see whats happening here. This told me something like "OutputStreamAppender[appender] - Encoder has not been set. Cannot invoke its init method".
After a little bit of testing i just moved line 19 ( appender.setEncoder(encoder); ) before line 16 ( appender.setContext(lc); ) and it worked!