001/**
002 * Copyright (c) 2004-2011 QOS.ch
003 * All rights reserved.
004 *
005 * Permission is hereby granted, free  of charge, to any person obtaining
006 * a  copy  of this  software  and  associated  documentation files  (the
007 * "Software"), to  deal in  the Software without  restriction, including
008 * without limitation  the rights to  use, copy, modify,  merge, publish,
009 * distribute,  sublicense, and/or sell  copies of  the Software,  and to
010 * permit persons to whom the Software  is furnished to do so, subject to
011 * the following conditions:
012 *
013 * The  above  copyright  notice  and  this permission  notice  shall  be
014 * included in all copies or substantial portions of the Software.
015 *
016 * THE  SOFTWARE IS  PROVIDED  "AS  IS", WITHOUT  WARRANTY  OF ANY  KIND,
017 * EXPRESS OR  IMPLIED, INCLUDING  BUT NOT LIMITED  TO THE  WARRANTIES OF
018 * MERCHANTABILITY,    FITNESS    FOR    A   PARTICULAR    PURPOSE    AND
019 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
020 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
021 * OF CONTRACT, TORT OR OTHERWISE,  ARISING FROM, OUT OF OR IN CONNECTION
022 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023 *
024 */
025package org.slf4j.bridge;
026
027import static org.junit.Assert.assertEquals;
028
029import java.text.MessageFormat;
030import java.util.ResourceBundle;
031import java.util.logging.Level;
032
033import org.apache.log4j.spi.LocationInfo;
034import org.apache.log4j.spi.LoggingEvent;
035import org.junit.After;
036import org.junit.Before;
037import org.junit.Test;
038
039public class SLF4JBridgeHandlerTest {
040
041    static String LOGGER_NAME = "yay";
042
043    ListAppender listAppender = new ListAppender();
044    org.apache.log4j.Logger log4jRoot;
045    java.util.logging.Logger julLogger = java.util.logging.Logger.getLogger(LOGGER_NAME);
046    java.util.logging.Logger julRootLogger = java.util.logging.Logger.getLogger("");
047    
048    @Before
049    public void setUp() throws Exception {
050        listAppender.extractLocationInfo = true;
051        log4jRoot = org.apache.log4j.Logger.getRootLogger();
052        log4jRoot.addAppender(listAppender);
053        log4jRoot.setLevel(org.apache.log4j.Level.TRACE);
054        julRootLogger.setLevel(Level.FINEST);
055    }
056
057    @After
058    public void tearDown() throws Exception {
059        SLF4JBridgeHandler.uninstall();
060        log4jRoot.getLoggerRepository().resetConfiguration();
061    }
062
063    @Test
064    public void testSmoke() {
065        SLF4JBridgeHandler.install();
066        
067        String msg = "msg";
068        julLogger.info(msg);
069        assertEquals(1, listAppender.list.size());
070        LoggingEvent le = (LoggingEvent) listAppender.list.get(0);
071        assertEquals(LOGGER_NAME, le.getLoggerName());
072        assertEquals(msg, le.getMessage());
073
074        // get the location info in the event.
075        // Note that this must have been computed previously
076        // within an appender for the following assertion to
077        // work properly
078        LocationInfo li = le.getLocationInformation();
079        System.out.println(li.fullInfo);
080        assertEquals("SLF4JBridgeHandlerTest.java", li.getFileName());
081        assertEquals("testSmoke", li.getMethodName());
082    }
083    
084    @Test
085    public void LOGBACK_1612() {
086        SLF4JBridgeHandler.install();
087        
088        String msg = "LOGBACK_1612";
089        julLogger.finer(msg);
090        assertEquals(1, listAppender.list.size());
091        LoggingEvent le = (LoggingEvent) listAppender.list.get(0);
092        assertEquals(LOGGER_NAME, le.getLoggerName());
093        assertEquals(msg, le.getMessage());
094
095    }
096
097    @Test
098    public void testLevels() {
099        SLF4JBridgeHandler.install();
100        String msg = "msg";
101        julLogger.setLevel(Level.ALL);
102
103        julLogger.finest(msg);
104        julLogger.finer(msg);
105        julLogger.fine(msg);
106        julLogger.info(msg);
107        julLogger.warning(msg);
108        julLogger.severe(msg);
109
110        assertEquals(6, listAppender.list.size());
111        int i = 0;
112        assertLevel(i++, org.apache.log4j.Level.TRACE);
113        assertLevel(i++, org.apache.log4j.Level.DEBUG);
114        assertLevel(i++, org.apache.log4j.Level.DEBUG);
115        assertLevel(i++, org.apache.log4j.Level.INFO);
116        assertLevel(i++, org.apache.log4j.Level.WARN);
117        assertLevel(i++, org.apache.log4j.Level.ERROR);
118    }
119
120   
121    
122    @Test
123    public void testLogWithResourceBundle() {
124        SLF4JBridgeHandler.install();
125
126        String resourceBundleName = "org.slf4j.bridge.testLogStrings";
127        ResourceBundle bundle = ResourceBundle.getBundle(resourceBundleName);
128        String resourceKey = "resource_key";
129        String expectedMsg = bundle.getString(resourceKey);
130        String msg = resourceKey;
131
132        java.util.logging.Logger julResourceBundleLogger = java.util.logging.Logger.getLogger("yay", resourceBundleName);
133
134        julResourceBundleLogger.info(msg);
135        assertEquals(1, listAppender.list.size());
136        LoggingEvent le = listAppender.list.get(0);
137        assertEquals(LOGGER_NAME, le.getLoggerName());
138        assertEquals(expectedMsg, le.getMessage());
139    }
140
141    @Test
142    public void testLogWithResourceBundleWithParameters() {
143        SLF4JBridgeHandler.install();
144
145        String resourceBundleName = "org.slf4j.bridge.testLogStrings";
146        ResourceBundle bundle = ResourceBundle.getBundle(resourceBundleName);
147
148        java.util.logging.Logger julResourceBundleLogger = java.util.logging.Logger.getLogger("foo", resourceBundleName);
149
150        String resourceKey1 = "resource_key_1";
151        String expectedMsg1 = bundle.getString(resourceKey1);
152        julResourceBundleLogger.info(resourceKey1); // 1st log
153
154        String resourceKey2 = "resource_key_2";
155        Object[] params2 = new Object[] { "foo", "bar" };
156        String expectedMsg2 = MessageFormat.format(bundle.getString(resourceKey2), params2);
157        julResourceBundleLogger.log(Level.INFO, resourceKey2, params2); // 2nd log
158
159        String resourceKey3 = "invalidKey {0}";
160        Object[] params3 = new Object[] { "John" };
161        String expectedMsg3 = MessageFormat.format(resourceKey3, params3);
162        julResourceBundleLogger.log(Level.INFO, resourceKey3, params3); // 3rd log
163
164        julLogger.log(Level.INFO, resourceKey3, params3); // 4th log
165
166        assertEquals(4, listAppender.list.size());
167
168        LoggingEvent le = null;
169
170        le = listAppender.list.get(0);
171        assertEquals("foo", le.getLoggerName());
172        assertEquals(expectedMsg1, le.getMessage());
173
174        le = listAppender.list.get(1);
175        assertEquals("foo", le.getLoggerName());
176        assertEquals(expectedMsg2, le.getMessage());
177
178        le = listAppender.list.get(2);
179        assertEquals("foo", le.getLoggerName());
180        assertEquals(expectedMsg3, le.getMessage());
181
182        le = listAppender.list.get(3);
183        assertEquals("yay", le.getLoggerName());
184        assertEquals(expectedMsg3, le.getMessage());
185    }
186
187    @Test
188    public void testLogWithPlaceholderNoParameters() {
189        SLF4JBridgeHandler.install();
190        String msg = "msg {non-number-string}";
191        julLogger.logp(Level.INFO, "SLF4JBridgeHandlerTest", "testLogWithPlaceholderNoParameters", msg, new Object[0]);
192
193        assertEquals(1, listAppender.list.size());
194        LoggingEvent le = listAppender.list.get(0);
195        assertEquals(LOGGER_NAME, le.getLoggerName());
196        assertEquals(msg, le.getMessage());
197    }
198
199    // See http://jira.qos.ch/browse/SLF4J-337
200
201    @Test
202    public void illFormattedInputShouldBeReturnedAsIs() {
203        SLF4JBridgeHandler.install();
204        String msg = "foo {18=bad} {0}";
205
206        julLogger.log(Level.INFO, msg, "ignored parameter due to IllegalArgumentException");
207        assertEquals(1, listAppender.list.size());
208        LoggingEvent le = listAppender.list.get(0);
209        assertEquals(msg, le.getMessage());
210    }
211
212    @Test
213    public void withNullMessage() {
214        SLF4JBridgeHandler.install();
215        String msg = null;
216        julLogger.log(Level.INFO, msg);
217        assertEquals(1, listAppender.list.size());
218        LoggingEvent le = listAppender.list.get(0);
219        assertEquals("", le.getMessage());
220    }
221
222    void assertLevel(int index, org.apache.log4j.Level expectedLevel) {
223        LoggingEvent le = listAppender.list.get(index);
224        assertEquals(expectedLevel, le.getLevel());
225    }
226}