Programmatic configuration of OutputStreamAppender

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

Hi Markos, Most (if not all) logback components must be started. Otherwise, they are in an inactive state and don't do any work. In your case, you need to call encoder.start() and logbackAppender.start(). By the way, when the doAppend() method of a non-started appender is invoked, it complains by adding a status message (via the context's status manager). Here the code which prints these messages on the console: StatusPrinter.print(logbackContext); These messages help when trying to figure out logback's internal state. -- Ceki http://tinyurl.com/proLogback On 18.07.2012 22:58, Markos Fragkakis wrote:
private static ByteArrayOutputStream baos = new ByteArrayOutputStream(100000000);
...
private void method() {
OutputStreamAppender<ILoggingEvent> logbackAppender = new OutputStreamAppender<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")
participants (2)
-
ceki
-
Markos Fragkakis