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