svn commit: r1878 - in logback/trunk/logback-classic/src: main/java/ch/qos/logback/classic/turbo test/input/joran test/java/ch/qos/logback/classic/joran

Author: ceki Date: Mon Oct 27 19:09:28 2008 New Revision: 1878 Added: logback/trunk/logback-classic/src/test/input/joran/turboDynamicThreshold2.xml Modified: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/turbo/DynamicThresholdFilter.java logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/turbo/MDCFilter.java logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/turbo/MDCValueLevelPair.java logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/turbo/MarkerFilter.java logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/turbo/MatchingFilter.java logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/turbo/TurboFilter.java logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/joran/JoranConfiguratorTest.java Log: Related to LBCLASSIC-53 - In DynamicThresholdFilter, renamed the OnMatch as OnHigherOrEquals, and OnMismatch as OnLower, while the new names may not be perfect, I think they are better than the old ones. - updated javadocs - added copyright notice Modified: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/turbo/DynamicThresholdFilter.java ============================================================================== --- logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/turbo/DynamicThresholdFilter.java (original) +++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/turbo/DynamicThresholdFilter.java Mon Oct 27 19:09:28 2008 @@ -10,60 +10,136 @@ import java.util.HashMap; /** - * This filter will allow you to associate threshold levels to values found - * in the MDC. The threshold/value associations are looked up in MDC using - * a key. This key can be any value specified by the user. - * - * <p>TO BE DISCUSSED... - * - * <p>This provides very efficient course grained filtering based on things like a - * product name or a company name that would be associated with requests as they - * are being processed. + * This filter allows for efficient course grained filtering based on criteria + * such as product name or company name that would be associated with requests + * as they are processed. * - * The example configuration below illustrates how debug logging could be - * enabled for only individual users. + * <p> This filter will allow you to associate threshold levels to a key put in + * the MDC. This key can be any value specified by the user. Furthermore, you + * can pass MDC value and level threshold associations, which are then looked up + * to find the level threshold to apply to the current logging request. If no + * level threshold could be found, then a 'default' value specified by the user + * is applied. We call this value 'levelAssociatedWithMDCValue'. + * + * <p> If 'levelAssociatedWithMDCValue' is higher or equal to the level of the + * current logger request, the + * {@link #decide(Marker, Logger, Level, String, Object[], Throwable) decide()} + * method returns the value of {@link #getOnHigherOrEqual() onHigherOrEqual}, + * if it is lower then the value of {@link #getOnLower() onLower} is returned. + * Both 'onHigherOrEqual' and 'onLower' can be set by the user. By default, + * 'onHigherOrEqual' is set to NEUTRAL and 'onLower' is set to DENY. Thus, if + * the current logger request's level is lower than + * 'levelAssociatedWithMDCValue', then the request is denied, and if it is + * higher or equal, then this filter decides NEUTRAL letting subsequent filters + * to make the decision on the fate of the logging request. + * + * <p> The example below illustrates how logging could be enabled for only + * individual users. In this example all events for logger names matching + * "com.mycompany" will be logged if they are for 'user1' and at a level higher + * than equals to DEBUG, and for 'user2' if they are at a level higher than or + * equal to TRACE, and for other users only if they are at level ERROR or + * higher. Events issued by loggers other than "com.mycompany" will only be + * logged if they are at level ERROR or higher since that is all the root logger + * allows. + * + * <pre> + * <configuration> + * <appender name="STDOUT" + * class="ch.qos.logback.core.ConsoleAppender"> + * <layout class="ch.qos.logback.classic.PatternLayout"> + * <Pattern>TEST %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</Pattern> + * </layout> + * </appender> + * + * <turboFilter class="ch.qos.logback.classic.turbo.DynamicThresholdFilter"> + * <Key>userId</Key> + * <DefaultTheshold>ERROR</DefaultTheshold> + * <MDCValueLevelPair> + * <value>user1</value> + * <level>DEBUG</level> + * </MDCValueLevelPair> + * <MDCValueLevelPair> + * <value>user2</value> + * <level>TRACE</level> + * </MDCValueLevelPair> + * </turboFilter> + * + * <logger name="com.mycompany" level="TRACE"/> + * + * <root level="ERROR" > + * <appender-ref ref="STDOUT" /> + * </root> + * </configuration> + * </pre> + * + * In the next configuration events from user1 and user2 will be logged + * regardless of the logger levels. Events for other users and records without a + * userid in the MDC will be logged if they are ERROR level messages. With this + * configuration, the root level is never checked since DynamicThresholdFilter + * will either accept or deny all records. * * <pre> - * <turboFilter class="ch.qos.logback.classic.turbo.DynamicThresholdFilter"> - * <Key>userId</Key> - * <DefaultTheshold>ERROR</DefaultTheshold> - * <MDCValueLevelPair> - * <value>user1</value> - * <level>DEBUG</level> - * </MDCValueLevelPair> - * <MDCValueLevelPair> - * <value>user2</value> - * <level>TRACE</level> - * </MDCValueLevelPair> - * </turboFilter> + * <configuration> + * <appender name="STDOUT" + * class="ch.qos.logback.core.ConsoleAppender"> + * <layout class="ch.qos.logback.classic.PatternLayout"> + * <Pattern>TEST %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</Pattern> + * </layout> + * </appender> + * + * <turboFilter class="ch.qos.logback.classic.turbo.DynamicThresholdFilter"> + * <Key>userId</Key> + * <DefaultTheshold>ERROR</DefaultTheshold> + * <OnHigherOrEqual>ACCEPT</OnHigherOrEqual> + * <OnLower>DENY</OnLower> + * <MDCValueLevelPair> + * <value>user1</value> + * <level>TRACE</level> + * </MDCValueLevelPair> + * <MDCValueLevelPair> + * <value>user2</value> + * <level>TRACE</level> + * </MDCValueLevelPair> + * </turboFilter> + * + * <root level="DEBUG" > + * <appender-ref ref="STDOUT" /> + * </root> + * </configuration> * </pre> * * @author Raplh Goers - * @author Ceki Gulcu + * @author Ceki Gülcü */ public class DynamicThresholdFilter extends TurboFilter { private Map<String, Level> valueLevelMap = new HashMap<String, Level>(); private Level defaultThreshold = Level.ERROR; private String key; + private FilterReply onHigherOrEqual = FilterReply.NEUTRAL; + private FilterReply onLower = FilterReply.DENY; + /** - * The MDC key that will be filtered against + * Get the MDC key whose value will be used as a level threshold * - * @param key - * The name of the key. + * @return the name of the MDC key. + */ + public String getKey() { + return this.key; + } + + /** + * @see setKey */ public void setKey(String key) { this.key = key; } /** + * Get the default threshold value when the MDC key is not set. * - * @return The name of the key being filtered + * @return the default threshold value in the absence of a set MDC key */ - public String getKey() { - return this.key; - } - public Level getDefaultThreshold() { return defaultThreshold; } @@ -73,6 +149,34 @@ } /** + * Get the FilterReply when the effective level is higher or equal to the + * level of current logging request + * + * @return FilterReply + */ + public FilterReply getOnHigherOrEqual() { + return onHigherOrEqual; + } + + public void setOnHigherOrEqual(FilterReply onHigherOrEqual) { + this.onHigherOrEqual = onHigherOrEqual; + } + + /** + * Get the FilterReply when the effective level is lower than the level of + * current logging request + * + * @return FilterReply + */ + public FilterReply getOnLower() { + return onLower; + } + + public void setOnLower(FilterReply onLower) { + this.onLower = onLower; + } + + /** * Add a new MDCValuePair */ public void addMDCValueLevelPair(MDCValueLevelPair mdcValueLevelPair) { @@ -96,6 +200,15 @@ } /** + * This method first finds the MDC value for 'key'. It then finds the level + * threshold associated with this MDC value from the list of MDCValueLevelPair + * passed to this filter. This value is stored in a variable called + * 'levelAssociatedWithMDCValue'. If it null, then it is set to the + * + * @{link #defaultThreshold} value. + * + * If no such value exists, then + * * * @param marker * @param logger @@ -103,16 +216,18 @@ * @param s * @param objects * @param throwable - * @return + * + * @return FilterReply - this filter's decision */ @Override public FilterReply decide(Marker marker, Logger logger, Level level, String s, Object[] objects, Throwable throwable) { + String mdcValue = MDC.get(this.key); - if(!isStarted()) { + if (!isStarted()) { return FilterReply.NEUTRAL; } - + Level levelAssociatedWithMDCValue = null; if (mdcValue != null) { levelAssociatedWithMDCValue = valueLevelMap.get(mdcValue); @@ -121,9 +236,9 @@ levelAssociatedWithMDCValue = defaultThreshold; } if (level.isGreaterOrEqual(levelAssociatedWithMDCValue)) { - return FilterReply.NEUTRAL; + return onHigherOrEqual; } else { - return FilterReply.DENY; + return onLower; } } } Modified: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/turbo/MDCFilter.java ============================================================================== --- logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/turbo/MDCFilter.java (original) +++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/turbo/MDCFilter.java Mon Oct 27 19:09:28 2008 @@ -1,3 +1,12 @@ +/** + * Logback: the generic, reliable, fast and flexible logging framework. + * + * Copyright (C) 2000-2008, QOS.ch + * + * This library is free software, you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation. + */ package ch.qos.logback.classic.turbo; import org.slf4j.MDC; @@ -10,18 +19,21 @@ /** * This class allows output for a given MDC value. * + * <p> * When the given value is identified by this TubroFilter, * the reply is based on the OnMatch option. * The information is taken from the MDC. For this TurboFilter to work, * one must set the key that will be used to * access the information in the MDC. * + * <p> * To allow output for the value, set the OnMatch option * to ACCEPT. To disable output for the given value, set * the OnMatch option to DENY. * + * <p> * By default, values of the OnMatch and OnMisMatch - * options are NEUTRAL. + * options are set to NEUTRAL. * * * @author Ceki Gülcü Modified: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/turbo/MDCValueLevelPair.java ============================================================================== --- logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/turbo/MDCValueLevelPair.java (original) +++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/turbo/MDCValueLevelPair.java Mon Oct 27 19:09:28 2008 @@ -1,3 +1,12 @@ +/** + * Logback: the generic, reliable, fast and flexible logging framework. + * + * Copyright (C) 2000-2008, QOS.ch + * + * This library is free software, you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation. + */ package ch.qos.logback.classic.turbo; import ch.qos.logback.classic.Level; @@ -7,7 +16,7 @@ * Bean pairing an MDC value with a log level. * * @author Raplh Goers - * @author Ceki Gulcu + * @author Ceki Gülcü */ public class MDCValueLevelPair { private String value; Modified: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/turbo/MarkerFilter.java ============================================================================== --- logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/turbo/MarkerFilter.java (original) +++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/turbo/MarkerFilter.java Mon Oct 27 19:09:28 2008 @@ -1,3 +1,12 @@ +/** + * Logback: the generic, reliable, fast and flexible logging framework. + * + * Copyright (C) 2000-2008, QOS.ch + * + * This library is free software, you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation. + */ package ch.qos.logback.classic.turbo; import org.slf4j.Marker; @@ -13,11 +22,11 @@ */ public class MarkerFilter extends MatchingFilter { - Marker marker2Match; + Marker markerToMatch; @Override public void start() { - if(marker2Match != null) { + if(markerToMatch != null) { super.start(); } else { addError("The marker property must be set for ["+getName()+"]"); @@ -34,7 +43,7 @@ return onMismatch; } - if(marker2Match.contains(marker)) { + if(markerToMatch.contains(marker)) { return onMatch; } else { return onMismatch; @@ -44,11 +53,11 @@ /** * The marker to match in the event. * - * @param marker2Match + * @param markerToMatch */ public void setMarker(String markerStr) { if(markerStr != null) { - this.marker2Match = MarkerFactory.getMarker(markerStr); + this.markerToMatch = MarkerFactory.getMarker(markerStr); } } } Modified: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/turbo/MatchingFilter.java ============================================================================== --- logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/turbo/MatchingFilter.java (original) +++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/turbo/MatchingFilter.java Mon Oct 27 19:09:28 2008 @@ -1,3 +1,12 @@ +/** + * Logback: the generic, reliable, fast and flexible logging framework. + * + * Copyright (C) 2000-2008, QOS.ch + * + * This library is free software, you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation. + */ package ch.qos.logback.classic.turbo; import ch.qos.logback.core.spi.FilterReply; Modified: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/turbo/TurboFilter.java ============================================================================== --- logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/turbo/TurboFilter.java (original) +++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/turbo/TurboFilter.java Mon Oct 27 19:09:28 2008 @@ -1,3 +1,12 @@ +/** + * Logback: the generic, reliable, fast and flexible logging framework. + * + * Copyright (C) 2000-2008, QOS.ch + * + * This library is free software, you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation. + */ package ch.qos.logback.classic.turbo; import org.slf4j.Marker; Added: logback/trunk/logback-classic/src/test/input/joran/turboDynamicThreshold2.xml ============================================================================== --- (empty file) +++ logback/trunk/logback-classic/src/test/input/joran/turboDynamicThreshold2.xml Mon Oct 27 19:09:28 2008 @@ -0,0 +1,32 @@ +<?xml version="1.0" encoding="UTF-8" ?> +<!DOCTYPE configuration> + +<configuration> + + <turboFilter + class="ch.qos.logback.classic.turbo.DynamicThresholdFilter"> + <Key>userId</Key> + <DefaultThreshold>ERROR</DefaultThreshold> + <OnMatch>ACCEPT</OnMatch> + <OnMismatch>DENY</OnMismatch> + <MDCValueLevelPair> + <value>user1</value> + <level>DEBUG</level> + </MDCValueLevelPair> + <MDCValueLevelPair> + <value>user2</value> + <level>TRACE</level> + </MDCValueLevelPair> + + </turboFilter> + + + <appender name="LIST" + class="ch.qos.logback.core.read.ListAppender"> + </appender> + + <root> + <level value="DEBUG" /> + <appender-ref ref="LIST" /> + </root> +</configuration> Modified: logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/joran/JoranConfiguratorTest.java ============================================================================== --- logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/joran/JoranConfiguratorTest.java (original) +++ logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/joran/JoranConfiguratorTest.java Mon Oct 27 19:09:28 2008 @@ -1,16 +1,19 @@ /** - * LOGBack: the generic, reliable, fast and flexible logging framework. - * - * Copyright (C) 1999-2006, QOS.ch - * - * This library is free software, you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation. + * Logback: the generic, reliable, fast and flexible logging framework. + * + * Copyright (C) 2000-2008, QOS.ch + * + * This library is free software, you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation. */ package ch.qos.logback.classic.joran; -import junit.framework.TestCase; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import org.junit.Test; import org.slf4j.MDC; import ch.qos.logback.classic.Level; @@ -26,20 +29,22 @@ import ch.qos.logback.core.read.ListAppender; import ch.qos.logback.core.util.StatusPrinter; -public class JoranConfiguratorTest extends TestCase { +public class JoranConfiguratorTest { - public JoranConfiguratorTest(String name) { - super(name); - } - - public void testSimpleList() throws JoranException { + LoggerContext loggerContext = new LoggerContext(); + Logger logger = loggerContext.getLogger(this.getClass().getName()); + Logger root = loggerContext.getLogger(LoggerContext.ROOT_NAME); + + void configure(String file) throws JoranException { JoranConfigurator jc = new JoranConfigurator(); - LoggerContext loggerContext = new LoggerContext(); jc.setContext(loggerContext); - jc.doConfigure(TeztConstants.TEST_DIR_PREFIX + "input/joran/simpleList.xml"); + jc.doConfigure(file); + } + + @Test + public void testSimpleList() throws JoranException { + configure(TeztConstants.TEST_DIR_PREFIX + "input/joran/simpleList.xml"); - //StatusPrinter.print(loggerContext.getStatusManager()); - Logger logger = loggerContext.getLogger(this.getClass().getName()); Logger root = loggerContext.getLogger(LoggerContext.ROOT_NAME); ListAppender listAppender = (ListAppender) root.getAppender("LIST"); @@ -50,158 +55,152 @@ LoggingEvent le = (LoggingEvent) listAppender.list.get(0); assertEquals(msg, le.getMessage()); } - - public void testLevel() throws JoranException { - JoranConfigurator jc = new JoranConfigurator(); - LoggerContext loggerContext = new LoggerContext(); - jc.setContext(loggerContext); - jc.doConfigure(TeztConstants.TEST_DIR_PREFIX + "input/joran/simpleLevel.xml"); - Logger logger = loggerContext.getLogger(this.getClass().getName()); - Logger root = loggerContext.getLogger(LoggerContext.ROOT_NAME); + @Test + public void testLevel() throws JoranException { + configure(TeztConstants.TEST_DIR_PREFIX + "input/joran/simpleLevel.xml"); ListAppender listAppender = (ListAppender) root.getAppender("LIST"); assertEquals(0, listAppender.list.size()); String msg = "hello world"; logger.debug(msg); assertEquals(0, listAppender.list.size()); } - + + @Test public void testStatusListener() throws JoranException { - JoranConfigurator jc = new JoranConfigurator(); - LoggerContext loggerContext = new LoggerContext(); - jc.setContext(loggerContext); - jc.doConfigure(TeztConstants.TEST_DIR_PREFIX + "input/joran/statusListener.xml"); - + configure(TeztConstants.TEST_DIR_PREFIX + "input/joran/statusListener.xml"); + StatusPrinter.print(loggerContext); } - + + @Test public void testEval() throws JoranException { - JoranConfigurator jc = new JoranConfigurator(); - LoggerContext loggerContext = new LoggerContext(); - jc.setContext(loggerContext); - jc.doConfigure(TeztConstants.TEST_DIR_PREFIX + "input/joran/callerData.xml"); + configure(TeztConstants.TEST_DIR_PREFIX + "input/joran/callerData.xml"); - //StatusPrinter.print(loggerContext); - - Logger logger = loggerContext.getLogger(this.getClass().getName()); String msg = "hello world"; logger.debug("toto"); logger.debug(msg); - - StringListAppender slAppender = (StringListAppender) loggerContext.getLogger("root").getAppender("STR_LIST"); + + StringListAppender slAppender = (StringListAppender) loggerContext + .getLogger("root").getAppender("STR_LIST"); assertNotNull(slAppender); assertEquals(2, slAppender.strList.size()); assertTrue(slAppender.strList.get(0).contains(" DEBUG - toto")); - + String str1 = slAppender.strList.get(1); assertTrue(str1.contains("Caller+0")); assertTrue(str1.contains(" DEBUG - hello world")); } - + + @Test public void testTurboFilter() throws JoranException { - //Although this test uses turbo filters, it only checks - //that Joran can see the xml element and create - //and place the relevant object correctly. - JoranConfigurator jc = new JoranConfigurator(); - LoggerContext loggerContext = new LoggerContext(); - jc.setContext(loggerContext); - jc.doConfigure(TeztConstants.TEST_DIR_PREFIX + "input/joran/turbo.xml"); + // Although this test uses turbo filters, it only checks + // that Joran can see the xml element and create + // and place the relevant object correctly. + configure(TeztConstants.TEST_DIR_PREFIX + "input/joran/turbo.xml"); - //StatusPrinter.print(loggerContext.getStatusManager()); - TurboFilter filter = loggerContext.getFirstTurboFilter(); assertTrue(filter instanceof NOPTurboFilter); } - + + @Test public void testTurboFilterWithStringList() throws JoranException { - //Although this test uses turbo filters, it only checks - //that Joran can see <user> elements, and behave correctly - //that is call the addUser method and pass the correct values - //to that method. - JoranConfigurator jc = new JoranConfigurator(); - LoggerContext loggerContext = new LoggerContext(); - jc.setContext(loggerContext); - jc.doConfigure(TeztConstants.TEST_DIR_PREFIX + "input/joran/turbo2.xml"); + // Although this test uses turbo filters, it only checks + // that Joran can see <user> elements, and behave correctly + // that is call the addUser method and pass the correct values + // to that method. + configure(TeztConstants.TEST_DIR_PREFIX + "input/joran/turbo2.xml"); + + // StatusPrinter.print(loggerContext.getStatusManager()); - //StatusPrinter.print(loggerContext.getStatusManager()); - TurboFilter filter = loggerContext.getFirstTurboFilter(); assertTrue(filter instanceof DebugUsersTurboFilter); - DebugUsersTurboFilter dutf = (DebugUsersTurboFilter)filter; + DebugUsersTurboFilter dutf = (DebugUsersTurboFilter) filter; assertEquals(2, dutf.getUsers().size()); } - + @Test public void testLevelFilter() throws JoranException { - JoranConfigurator jc = new JoranConfigurator(); - LoggerContext loggerContext = new LoggerContext(); - jc.setContext(loggerContext); - jc.doConfigure(TeztConstants.TEST_DIR_PREFIX - + "input/joran/levelFilter.xml"); + configure(TeztConstants.TEST_DIR_PREFIX + "input/joran/levelFilter.xml"); StatusPrinter.print(loggerContext); - Logger logger = loggerContext.getLogger(this.getClass().getName()); logger.warn("hello"); logger.error("to be ignored"); - + @SuppressWarnings("unchecked") - ListAppender<LoggingEvent> listAppender = (ListAppender) loggerContext - .getLogger("root").getAppender("LIST"); - + ListAppender<LoggingEvent> listAppender = (ListAppender) root + .getAppender("LIST"); + assertNotNull(listAppender); assertEquals(1, listAppender.list.size()); LoggingEvent back = listAppender.list.get(0); assertEquals(Level.WARN, back.getLevel()); assertEquals("hello", back.getMessage()); } - + + @Test public void testEvaluatorFilter() throws JoranException { - JoranConfigurator jc = new JoranConfigurator(); - LoggerContext loggerContext = new LoggerContext(); - jc.setContext(loggerContext); - jc.doConfigure(TeztConstants.TEST_DIR_PREFIX - + "input/joran/evaluatorFilter.xml"); + configure(TeztConstants.TEST_DIR_PREFIX + "input/joran/evaluatorFilter.xml"); StatusPrinter.print(loggerContext); - Logger logger = loggerContext.getLogger(this.getClass().getName()); logger.warn("hello"); logger.error("to be ignored"); - + @SuppressWarnings("unchecked") - ListAppender<LoggingEvent> listAppender = (ListAppender) loggerContext - .getLogger("root").getAppender("LIST"); - + ListAppender<LoggingEvent> listAppender = (ListAppender) root + .getAppender("LIST"); + assertNotNull(listAppender); assertEquals(1, listAppender.list.size()); LoggingEvent back = listAppender.list.get(0); assertEquals(Level.WARN, back.getLevel()); assertEquals("hello", back.getMessage()); } - - public void testTurboDynamicThreshold() throws JoranException { - JoranConfigurator jc = new JoranConfigurator(); - LoggerContext loggerContext = new LoggerContext(); - jc.setContext(loggerContext); - jc.doConfigure(TeztConstants.TEST_DIR_PREFIX + "input/joran/turboDynamicThreshold.xml"); - StatusPrinter.print(loggerContext.getStatusManager()); - - Logger logger = loggerContext.getLogger(this.getClass().getName()); - Logger root = loggerContext.getLogger(LoggerContext.ROOT_NAME); + @Test + public void testTurboDynamicThreshold() throws JoranException { + configure(TeztConstants.TEST_DIR_PREFIX + + "input/joran/turboDynamicThreshold.xml"); + ListAppender listAppender = (ListAppender) root.getAppender("LIST"); assertEquals(0, listAppender.list.size()); - + // this one should be denied MDC.put("userId", "user1"); logger.debug("hello user1"); // this one should log MDC.put("userId", "user2"); - logger.debug("hello user2"); - + logger.debug("hello user2"); + assertEquals(1, listAppender.list.size()); LoggingEvent le = (LoggingEvent) listAppender.list.get(0); assertEquals("hello user2", le.getMessage()); } + + @Test + public void testTurboDynamicThreshold2() throws JoranException { + configure(TeztConstants.TEST_DIR_PREFIX + + "input/joran/turboDynamicThreshold2.xml"); + + ListAppender listAppender = (ListAppender) root.getAppender("LIST"); + assertEquals(0, listAppender.list.size()); + + // this one should log + MDC.put("userId", "user1"); + logger.debug("hello user1"); + // this one should log + MDC.put("userId", "user2"); + logger.debug("hello user2"); + // this one should fail + MDC.put("userId", "user3"); + logger.debug("hello user3"); + + assertEquals(2, listAppender.list.size()); + LoggingEvent le = (LoggingEvent) listAppender.list.get(0); + assertEquals("hello user1", le.getMessage()); + le = (LoggingEvent) listAppender.list.get(1); + assertEquals("hello user2", le.getMessage()); + } }
participants (1)
-
noreply.ceki@qos.ch