[logback-user] Programmatic configuration of OutputStreamAppender

ceki ceki at qos.ch
Wed Jul 18 23:25:18 CEST 2012


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")




More information about the Logback-user mailing list