[logback-user] Programmatic configuration of OutputStreamAppender

Markos Fragkakis markos.fragkakis at gmail.com
Wed Jul 18 22:58:47 CEST 2012


Hi all,

I am trying to configure Logback programmatically to write logs into an
OutputStream.

The following libraries are in my classpath:
* logback-core-1.0.6.jar
* logback-classic-1.0.6.jar
* slf4j-api-1.6.4.jar

In fact, I _do_ get the logs in the console, which tells me that Logback
has started with the default configuration of outputting to the Console.

What I want, however, is to programmatically add an appender to a Logger,
so that it also outputs to an OutputStream.

The following snippet shows what I am doing, and the result is only writing
logs to the Console:

Any ideas what I'm doing wrong?

 private static ByteArrayOutputStream baos =
newByteArrayOutputStream(100000000);

 ...

private void method() {

OutputStreamAppender<ILoggingEvent> logbackAppender =
newOutputStreamAppender<ILoggingEvent>();

 LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();


 PatternLayoutEncoder encoder = new PatternLayoutEncoder();

 encoder.setContext(lc);

 encoder.setPattern("%-5level [%thread]: %message%n");


   logbackAppender.setContext(lc);

 logbackAppender.setName("Output stream logback appender");

 logbackAppender.setOutputStream(baos);

logbackAppender.setEncoder(encoder);


LOGGER.addAppender(logbackAppender);

LOGGER.debug("This");
LOGGER.info("can't be");
LOGGER.info("happening")

}





-- 
Sent from my iPhone
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.qos.ch/pipermail/logback-user/attachments/20120718/882824dc/attachment.html>


More information about the Logback-user mailing list