Skip to content

Commit

Permalink
addtional turbofilter test using the fluent api
Browse files Browse the repository at this point in the history
Signed-off-by: Ceki Gulcu <[email protected]>
  • Loading branch information
ceki committed Dec 13, 2022
1 parent 8fd2b3a commit 0872868
Showing 1 changed file with 49 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,16 @@
*/
package ch.qos.logback.classic;

import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.classic.turbo.MDCFilter;
import ch.qos.logback.classic.turbo.MarkerFilter;
import ch.qos.logback.classic.turbo.TurboFilter;
import ch.qos.logback.core.read.ListAppender;
import ch.qos.logback.core.spi.FilterReply;
import ch.qos.logback.core.testUtil.RandomUtil;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.slf4j.MDC;
import org.slf4j.Marker;
import org.slf4j.MarkerFactory;

Expand All @@ -29,44 +34,66 @@
public class TurboFilteringInLoggerTest {

static final String BLUE = "BLUE";
LoggerContext context;
LoggerContext loggerContext;
Logger logger;
Marker blueMarker = MarkerFactory.getMarker(BLUE);

int diff = RandomUtil.getPositiveInt();
String key = "tfiolKey" + diff;
String value = "val" + diff;

ListAppender<ILoggingEvent> listAppender = new ListAppender<>();

MDCFilter mdcFilter = new MDCFilter();
@BeforeEach
public void setUp() throws Exception {
context = new LoggerContext();
context.setName("test");
context.start();
logger = context.getLogger(TurboFilteringInLoggerTest.class);
loggerContext = new LoggerContext();
loggerContext.setName("test");
loggerContext.start();
logger = loggerContext.getLogger(TurboFilteringInLoggerTest.class);

Logger root = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME);
root.setLevel(Level.ERROR);
listAppender.start();
root.addAppender(listAppender);

}

private void addMDCFilter() {

mdcFilter.setOnMatch("ACCEPT");
mdcFilter.setOnMismatch("DENY");
mdcFilter.setMDCKey(key);
mdcFilter.setValue(value);
mdcFilter.start();
loggerContext.addTurboFilter(mdcFilter);
}
private void addYesFilter() {
YesFilter filter = new YesFilter();
filter.start();
context.addTurboFilter(filter);
loggerContext.addTurboFilter(filter);
}

private void addNoFilter() {
NoFilter filter = new NoFilter();
filter.start();
context.addTurboFilter(filter);
loggerContext.addTurboFilter(filter);
}

private void addAcceptBLUEFilter() {
MarkerFilter filter = new MarkerFilter();
filter.setMarker(BLUE);
filter.setOnMatch("ACCEPT");
filter.start();
context.addTurboFilter(filter);
loggerContext.addTurboFilter(filter);
}

private void addDenyBLUEFilter() {
MarkerFilter filter = new MarkerFilter();
filter.setMarker(BLUE);
filter.setOnMatch("DENY");
filter.start();
context.addTurboFilter(filter);
loggerContext.addTurboFilter(filter);
}

@Test
Expand Down Expand Up @@ -156,11 +183,21 @@ public void testIsErrorEnabledWithDenyBlueFilter() {
@Test
public void testLoggingContextReset() {
addYesFilter();
assertNotNull(context.getTurboFilterList().get(0));
context.reset();
assertEquals(0, context.getTurboFilterList().size());
assertNotNull(loggerContext.getTurboFilterList().get(0));
loggerContext.reset();
assertEquals(0, loggerContext.getTurboFilterList().size());
}

@Test
public void fluentAPI() {
addMDCFilter();
Logger logger = loggerContext.getLogger(this.getClass());
logger.atDebug().log("hello 1");
assertEquals(0, listAppender.list.size());
MDC.put(key, value);
logger.atDebug().log("hello 2");
assertEquals(1, listAppender.list.size());
}
}

class YesFilter extends TurboFilter {
Expand Down

0 comments on commit 0872868

Please sign in to comment.