Hello Claus,

A slightly easier way would be to use a ListAppender, which appends each event 
into a ...list. Logback-core has an implementation of about 10 lines each which 
is extensively for testing purposes in the other modules.

Here it is:

package ch.qos.logback.core.read;

import java.util.ArrayList;
import java.util.List;

import ch.qos.logback.core.AppenderBase;

public class ListAppender<E> extends AppenderBase<E> {

   public List<E> list = new ArrayList<E>();

   protected void append(E e) {
     list.add(e);
   }
}

Check the tests where ListAppender is used. I think BasicLoggerTest should give 
you a good idea of usage.



HTH,

Claus Nielsen wrote:
> I have a unit test which checks that a class writes the log statements 
> it's supposed to.
> 
> The following code works, but I was wondering if there isn't a better or 
> easier way to do this.
> 
> Here's what I have now:
> 
> public void testCloseChannels() {
>      Logger rootLogger = (Logger)
>          LoggerFactory.getLogger(
>              LoggerContext.ROOT_NAME);
>      Appender<LoggingEvent> appender =
>          rootLogger.getAppender("console");
>      appender.addFilter(new Filter() {
>          @Override
>          public FilterReply decide(Object o) {
>              LoggingEvent event = (LoggingEvent) o;
>              if (event.getMessage().equals("Failed to close channel.")) {
>                  expectedLogEntryCount.incrementAndGet();
>                  return FilterReply.DENY;
>              }
>              return FilterReply.NEUTRAL;
>          }
>      });
>      close(null, new UnclosableChannelStub(), null,
>          new ChannelStub(), new ChannelStub());
>      assertEquals("Exactly 1 failure should have been logged.",
>          1, expectedLogEntryCount.intValue());
>      appender.clearAllFilters();
> }
> 
> 
> Regards,
> Claus Nielsen

-- 
--
Ceki Gülcü
QOS.ch is looking to hire talented developers located in Switzerland.
If interested, please contact c e k i AT q o s . c h
_______________________________________________
Logback-user mailing list
[email protected]
http://qos.ch/mailman/listinfo/logback-user

Reply via email to