001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.classic;
015
016import static org.junit.Assert.assertEquals;
017import static org.junit.Assert.assertFalse;
018import static org.junit.Assert.assertNotNull;
019import static org.junit.Assert.assertTrue;
020import static org.junit.Assert.fail;
021
022import java.util.List;
023
024import org.junit.Test;
025import org.slf4j.LoggerFactory;
026
027import ch.qos.logback.classic.spi.ILoggingEvent;
028import ch.qos.logback.core.read.ListAppender;
029import ch.qos.logback.core.status.Status;
030
031public class LoggerTest {
032
033    LoggerContext lc = new LoggerContext();
034    Logger root = lc.getLogger(Logger.ROOT_LOGGER_NAME);
035    Logger loggerTest = lc.getLogger(LoggerTest.class);
036
037    ListAppender<ILoggingEvent> listAppender = new ListAppender<ILoggingEvent>();
038
039    @Test
040    public void smoke() {
041        ListAppender<ILoggingEvent> listAppender = new ListAppender<ILoggingEvent>();
042        listAppender.start();
043        root.addAppender(listAppender);
044        Logger logger = lc.getLogger(LoggerTest.class);
045        assertEquals(0, listAppender.list.size());
046        logger.debug("hello");
047        assertEquals(1, listAppender.list.size());
048    }
049
050    @Test
051    public void testNoStart() {
052        // listAppender.start();
053        listAppender.setContext(lc);
054        root.addAppender(listAppender);
055        Logger logger = lc.getLogger(LoggerTest.class);
056        logger.debug("hello");
057
058        List<Status> statusList = lc.getStatusManager().getCopyOfStatusList();
059        Status s0 = statusList.get(0);
060        assertEquals(Status.WARN, s0.getLevel());
061        assertTrue(s0.getMessage().startsWith("Attempted to append to non started"));
062    }
063
064    @Test
065    public void testAdditive() {
066        listAppender.start();
067        root.addAppender(listAppender);
068        loggerTest.addAppender(listAppender);
069        loggerTest.setAdditive(false);
070        loggerTest.debug("hello");
071        // 1 instead of two, since logger is not additive
072        assertEquals(1, listAppender.list.size());
073    }
074
075    @Test
076    public void testRootLogger() {
077        Logger logger = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
078        LoggerContext lc = logger.getLoggerContext();
079
080        assertNotNull("Returned logger is null", logger);
081        assertEquals("Return logger isn't named root", logger.getName(), Logger.ROOT_LOGGER_NAME);
082        assertTrue("logger instances should be indentical", logger == lc.root);
083    }
084
085    @Test
086    public void testBasicFiltering() throws Exception {
087        listAppender.start();
088        root.addAppender(listAppender);
089        root.setLevel(Level.INFO);
090        loggerTest.debug("x");
091        assertEquals(0, listAppender.list.size());
092        loggerTest.info("x");
093        loggerTest.warn("x");
094        loggerTest.error("x");
095        assertEquals(3, listAppender.list.size());
096    }
097
098    void checkLevelThreshold(Logger logger, Level threshold) {
099
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}