View Javadoc
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  
17  import java.util.List;
18  
19  import org.junit.jupiter.api.Test;
20  import org.slf4j.LoggerFactory;
21  
22  import ch.qos.logback.classic.spi.ILoggingEvent;
23  import ch.qos.logback.core.read.ListAppender;
24  import ch.qos.logback.core.status.Status;
25  import org.slf4j.spi.LoggingEventBuilder;
26  import org.slf4j.spi.NOPLoggingEventBuilder;
27  
28  import static org.junit.jupiter.api.Assertions.assertEquals;
29  import static org.junit.jupiter.api.Assertions.assertFalse;
30  import static org.junit.jupiter.api.Assertions.assertNotNull;
31  import static org.junit.jupiter.api.Assertions.assertTrue;
32  import static org.junit.jupiter.api.Assertions.fail;
33  
34  public class LoggerTest {
35  
36      LoggerContext lc = new LoggerContext();
37      Logger root = lc.getLogger(Logger.ROOT_LOGGER_NAME);
38      Logger loggerTest = lc.getLogger(LoggerTest.class);
39  
40      ListAppender<ILoggingEvent> listAppender = new ListAppender<ILoggingEvent>();
41  
42      @Test
43      public void smoke() {
44          ListAppender<ILoggingEvent> listAppender = new ListAppender<ILoggingEvent>();
45          listAppender.start();
46          root.addAppender(listAppender);
47          Logger logger = lc.getLogger(LoggerTest.class);
48          assertEquals(0, listAppender.list.size());
49          logger.debug("hello");
50          assertEquals(1, listAppender.list.size());
51      }
52  
53      @Test
54      public void testNoStart() {
55          // listAppender.start();
56          listAppender.setContext(lc);
57          root.addAppender(listAppender);
58          Logger logger = lc.getLogger(LoggerTest.class);
59          logger.debug("hello");
60  
61          List<Status> statusList = lc.getStatusManager().getCopyOfStatusList();
62          Status s0 = statusList.get(0);
63          assertEquals(Status.WARN, s0.getLevel());
64          assertTrue(s0.getMessage().startsWith("Attempted to append to non started"));
65      }
66  
67      @Test
68      public void testAdditive() {
69          listAppender.start();
70          root.addAppender(listAppender);
71          loggerTest.addAppender(listAppender);
72          loggerTest.setAdditive(false);
73          loggerTest.debug("hello");
74          // 1 instead of two, since logger is not additive
75          assertEquals(1, listAppender.list.size());
76      }
77  
78      @Test
79      public void testRootLogger() {
80          Logger logger = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
81          LoggerContext lc = logger.getLoggerContext();
82  
83          assertNotNull(logger, "Returned logger is null");
84          assertEquals(logger.getName(), Logger.ROOT_LOGGER_NAME, "Return logger isn't named root");
85          assertTrue(logger == lc.root, "logger instances should be indentical");
86      }
87  
88      @Test
89      public void testBasicFiltering() throws Exception {
90          listAppender.start();
91          root.addAppender(listAppender);
92          root.setLevel(Level.INFO);
93          loggerTest.debug("x");
94          assertEquals(0, listAppender.list.size());
95          loggerTest.info("x");
96          loggerTest.warn("x");
97          loggerTest.error("x");
98          assertEquals(3, listAppender.list.size());
99      }
100 
101     void checkLevelThreshold(Logger logger, Level threshold) {
102 
103         if (Level.ERROR_INT >= threshold.levelInt) {
104             assertTrue(logger.isErrorEnabled());
105             assertTrue(logger.isEnabledFor(Level.ERROR));
106         } else {
107             assertFalse(logger.isErrorEnabled());
108             assertFalse(logger.isEnabledFor(Level.ERROR));
109         }
110 
111         if (Level.WARN_INT >= threshold.levelInt) {
112             assertTrue(logger.isWarnEnabled());
113             assertTrue(logger.isEnabledFor(Level.WARN));
114         } else {
115             assertFalse(logger.isWarnEnabled());
116             assertFalse(logger.isEnabledFor(Level.WARN));
117         }
118         if (Level.INFO_INT >= threshold.levelInt) {
119             assertTrue(logger.isInfoEnabled());
120             assertTrue(logger.isEnabledFor(Level.INFO));
121         } else {
122             assertFalse(logger.isInfoEnabled());
123             assertFalse(logger.isEnabledFor(Level.INFO));
124         }
125         if (Level.DEBUG_INT >= threshold.levelInt) {
126             assertTrue(logger.isDebugEnabled());
127             assertTrue(logger.isEnabledFor(Level.DEBUG));
128         } else {
129             assertFalse(logger.isDebugEnabled());
130             assertFalse(logger.isEnabledFor(Level.DEBUG));
131         }
132         if (Level.TRACE_INT >= threshold.levelInt) {
133             assertTrue(logger.isTraceEnabled());
134             assertTrue(logger.isEnabledFor(Level.TRACE));
135         } else {
136             assertFalse(logger.isTraceEnabled());
137             assertFalse(logger.isEnabledFor(Level.TRACE));
138         }
139     }
140 
141     @Test
142     public void innerClass_I() {
143         root.setLevel(Level.DEBUG);
144         Logger a = lc.getLogger("a");
145         a.setLevel(Level.INFO);
146         Logger a_b = lc.getLogger("a$b");
147         assertEquals(Level.INFO, a_b.getEffectiveLevel());
148     }
149 
150     @Test
151     public void innerClass_II() {
152         root.setLevel(Level.DEBUG);
153         Logger a = lc.getLogger(this.getClass());
154         a.setLevel(Level.INFO);
155         Logger a_b = lc.getLogger(new Inner().getClass());
156         assertEquals(Level.INFO, a_b.getEffectiveLevel());
157     }
158 
159     class Inner {
160     }
161 
162     @Test
163     public void testEnabled_All() throws Exception {
164         root.setLevel(Level.ALL);
165         checkLevelThreshold(loggerTest, Level.ALL);
166     }
167 
168     @Test
169     public void fluentAPIAtDisabledDebugLevelShouldReturnNOPLoggingEventBuilder() throws Exception {
170         root.setLevel(Level.INFO);
171         LoggingEventBuilder leb = loggerTest.atLevel(org.slf4j.event.Level.DEBUG);
172         assertEquals(NOPLoggingEventBuilder.class, leb.getClass());
173     }
174 
175     @Test
176     public void testEnabled_Debug() throws Exception {
177         root.setLevel(Level.DEBUG);
178         checkLevelThreshold(loggerTest, Level.DEBUG);
179     }
180 
181     @Test
182     public void testEnabled_Info() throws Exception {
183         root.setLevel(Level.INFO);
184         checkLevelThreshold(loggerTest, Level.INFO);
185     }
186 
187     @Test
188     public void testEnabledX_Warn() throws Exception {
189         root.setLevel(Level.WARN);
190         checkLevelThreshold(loggerTest, Level.WARN);
191     }
192 
193     public void testEnabledX_Errror() throws Exception {
194         root.setLevel(Level.ERROR);
195         checkLevelThreshold(loggerTest, Level.ERROR);
196     }
197 
198     @Test
199     public void testEnabledX_Off() throws Exception {
200         root.setLevel(Level.OFF);
201         checkLevelThreshold(loggerTest, Level.OFF);
202     }
203 
204     @Test
205     public void setRootLevelToNull() {
206         try {
207             root.setLevel(null);
208             fail("The level of the root logger should not be settable to null");
209         } catch (IllegalArgumentException e) {
210         }
211     }
212 
213     @Test
214     public void setLevelToNull_A() {
215         loggerTest.setLevel(null);
216         assertEquals(root.getEffectiveLevel(), loggerTest.getEffectiveLevel());
217     }
218 
219     @Test
220     public void setLevelToNull_B() {
221         loggerTest.setLevel(Level.DEBUG);
222         loggerTest.setLevel(null);
223         assertEquals(root.getEffectiveLevel(), loggerTest.getEffectiveLevel());
224     }
225 
226     @Test
227     public void setLevelToNull_LBCLASSIC_91() {
228         loggerTest.setLevel(Level.DEBUG);
229         ch.qos.logback.classic.Logger child = lc.getLogger(loggerTest.getName() + ".child");
230         loggerTest.setLevel(null);
231         assertEquals(root.getEffectiveLevel(), loggerTest.getEffectiveLevel());
232         assertEquals(root.getEffectiveLevel(), child.getEffectiveLevel());
233     }
234 
235 }