1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.classic;
15  
16  import ch.qos.logback.classic.spi.ILoggingEvent;
17  import ch.qos.logback.classic.turbo.MDCFilter;
18  import ch.qos.logback.classic.turbo.MarkerFilter;
19  import ch.qos.logback.classic.turbo.TurboFilter;
20  import ch.qos.logback.core.read.ListAppender;
21  import ch.qos.logback.core.spi.FilterReply;
22  import ch.qos.logback.core.testUtil.RandomUtil;
23  import org.junit.jupiter.api.BeforeEach;
24  import org.junit.jupiter.api.Test;
25  import org.slf4j.MDC;
26  import org.slf4j.Marker;
27  import org.slf4j.MarkerFactory;
28  
29  import static org.junit.jupiter.api.Assertions.assertEquals;
30  import static org.junit.jupiter.api.Assertions.assertFalse;
31  import static org.junit.jupiter.api.Assertions.assertNotNull;
32  import static org.junit.jupiter.api.Assertions.assertTrue;
33  
34  public class TurboFilteringInLoggerTest {
35  
36      static final String BLUE = "BLUE";
37      LoggerContext loggerContext;
38      Logger logger;
39      Marker blueMarker = MarkerFactory.getMarker(BLUE);
40  
41      int diff = RandomUtil.getPositiveInt();
42      String key = "tfiolKey" + diff;
43      String value = "val" + diff;
44  
45      ListAppender<ILoggingEvent> listAppender = new ListAppender<>();
46  
47      MDCFilter mdcFilter = new MDCFilter();
48      @BeforeEach
49      public void setUp() throws Exception {
50          loggerContext = new LoggerContext();
51          loggerContext.setName("test");
52          loggerContext.start();
53          logger = loggerContext.getLogger(TurboFilteringInLoggerTest.class);
54  
55          Logger root = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME);
56          root.setLevel(Level.ERROR);
57          listAppender.start();
58          root.addAppender(listAppender);
59  
60      }
61  
62      private void addMDCFilter() {
63  
64          mdcFilter.setOnMatch("ACCEPT");
65          mdcFilter.setOnMismatch("DENY");
66          mdcFilter.setMDCKey(key);
67          mdcFilter.setValue(value);
68          mdcFilter.start();
69          loggerContext.addTurboFilter(mdcFilter);
70      }
71      private void addYesFilter() {
72          YesFilter filter = new YesFilter();
73          filter.start();
74          loggerContext.addTurboFilter(filter);
75      }
76  
77      private void addNoFilter() {
78          NoFilter filter = new NoFilter();
79          filter.start();
80          loggerContext.addTurboFilter(filter);
81      }
82  
83      private void addAcceptBLUEFilter() {
84          MarkerFilter filter = new MarkerFilter();
85          filter.setMarker(BLUE);
86          filter.setOnMatch("ACCEPT");
87          filter.start();
88          loggerContext.addTurboFilter(filter);
89      }
90  
91      private void addDenyBLUEFilter() {
92          MarkerFilter filter = new MarkerFilter();
93          filter.setMarker(BLUE);
94          filter.setOnMatch("DENY");
95          filter.start();
96          loggerContext.addTurboFilter(filter);
97      }
98  
99      @Test
100     public void testIsDebugEnabledWithYesFilter() {
101         addYesFilter();
102         logger.setLevel(Level.INFO);
103         assertTrue(logger.isDebugEnabled());
104     }
105 
106     @Test
107     public void testIsInfoEnabledWithYesFilter() {
108         addYesFilter();
109         logger.setLevel(Level.WARN);
110         assertTrue(logger.isInfoEnabled());
111     }
112 
113     @Test
114     public void testIsWarnEnabledWithYesFilter() {
115         addYesFilter();
116         logger.setLevel(Level.ERROR);
117         assertTrue(logger.isWarnEnabled());
118     }
119 
120     @Test
121     public void testIsErrorEnabledWithYesFilter() {
122         addYesFilter();
123         logger.setLevel(Level.OFF);
124         assertTrue(logger.isErrorEnabled());
125     }
126 
127     @Test
128     public void testIsEnabledForWithYesFilter() {
129         addYesFilter();
130         logger.setLevel(Level.ERROR);
131         assertTrue(logger.isEnabledFor(Level.INFO));
132     }
133 
134     @Test
135     public void testIsEnabledForWithNoFilter() {
136         addNoFilter();
137         logger.setLevel(Level.DEBUG);
138         assertFalse(logger.isEnabledFor(Level.INFO));
139     }
140 
141     @Test
142     public void testIsDebugEnabledWithNoFilter() {
143         addNoFilter();
144         logger.setLevel(Level.DEBUG);
145         assertFalse(logger.isDebugEnabled());
146     }
147 
148     @Test
149     public void testIsInfoEnabledWithNoFilter() {
150         addNoFilter();
151         logger.setLevel(Level.DEBUG);
152         assertFalse(logger.isInfoEnabled());
153     }
154 
155     @Test
156     public void testIsWarnEnabledWithNoFilter() {
157         addNoFilter();
158         logger.setLevel(Level.DEBUG);
159         assertFalse(logger.isWarnEnabled());
160     }
161 
162     @Test
163     public void testIsErrorEnabledWithNoFilter() {
164         addNoFilter();
165         logger.setLevel(Level.DEBUG);
166         assertFalse(logger.isErrorEnabled());
167     }
168 
169     @Test
170     public void testIsErrorEnabledWithAcceptBlueFilter() {
171         addAcceptBLUEFilter();
172         logger.setLevel(Level.ERROR);
173         assertTrue(logger.isDebugEnabled(blueMarker));
174     }
175 
176     @Test
177     public void testIsErrorEnabledWithDenyBlueFilter() {
178         addDenyBLUEFilter();
179         logger.setLevel(Level.ALL);
180         assertFalse(logger.isDebugEnabled(blueMarker));
181     }
182 
183     @Test
184     public void testLoggingContextReset() {
185         addYesFilter();
186         assertNotNull(loggerContext.getTurboFilterList().get(0));
187         loggerContext.reset();
188         assertEquals(0, loggerContext.getTurboFilterList().size());
189     }
190 
191     @Test
192     public void fluentAPI() {
193         addMDCFilter();
194         Logger logger = loggerContext.getLogger(this.getClass());
195         logger.atDebug().log("hello 1");
196         assertEquals(0, listAppender.list.size());
197         MDC.put(key, value);
198         logger.atDebug().log("hello 2");
199         assertEquals(1, listAppender.list.size());
200     }
201 }
202 
203 class YesFilter extends TurboFilter {
204     @Override
205     public FilterReply decide(Marker marker, Logger logger, Level level, String format, Object[] params, Throwable t) {
206         return FilterReply.ACCEPT;
207     }
208 }
209 
210 class NoFilter extends TurboFilter {
211     @Override
212     public FilterReply decide(Marker marker, Logger logger, Level level, String format, Object[] params, Throwable t) {
213         return FilterReply.DENY;
214     }
215 }