• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2004-2011 QOS.ch
3  * All rights reserved.
4  *
5  * Permission is hereby granted, free  of charge, to any person obtaining
6  * a  copy  of this  software  and  associated  documentation files  (the
7  * "Software"), to  deal in  the Software without  restriction, including
8  * without limitation  the rights to  use, copy, modify,  merge, publish,
9  * distribute,  sublicense, and/or sell  copies of  the Software,  and to
10  * permit persons to whom the Software  is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The  above  copyright  notice  and  this permission  notice  shall  be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE  SOFTWARE IS  PROVIDED  "AS  IS", WITHOUT  WARRANTY  OF ANY  KIND,
17  * EXPRESS OR  IMPLIED, INCLUDING  BUT NOT LIMITED  TO THE  WARRANTIES OF
18  * MERCHANTABILITY,    FITNESS    FOR    A   PARTICULAR    PURPOSE    AND
19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21  * OF CONTRACT, TORT OR OTHERWISE,  ARISING FROM, OUT OF OR IN CONNECTION
22  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */
25 package org.slf4j.bridge;
26 
27 import static org.junit.Assert.assertEquals;
28 
29 import java.text.MessageFormat;
30 import java.util.ResourceBundle;
31 import java.util.logging.Level;
32 
33 import org.apache.log4j.spi.LocationInfo;
34 import org.apache.log4j.spi.LoggingEvent;
35 import org.junit.After;
36 import org.junit.Before;
37 import org.junit.Test;
38 
39 public class SLF4JBridgeHandlerTest {
40 
41     static String LOGGER_NAME = "yay";
42 
43     ListAppender listAppender = new ListAppender();
44     org.apache.log4j.Logger log4jRoot;
45     java.util.logging.Logger julLogger = java.util.logging.Logger.getLogger(LOGGER_NAME);
46     java.util.logging.Logger julRootLogger = java.util.logging.Logger.getLogger("");
47 
48     @Before
setUp()49     public void setUp() throws Exception {
50         listAppender.extractLocationInfo = true;
51         log4jRoot = org.apache.log4j.Logger.getRootLogger();
52         log4jRoot.addAppender(listAppender);
53         log4jRoot.setLevel(org.apache.log4j.Level.TRACE);
54         julRootLogger.setLevel(Level.FINEST);
55     }
56 
57     @After
tearDown()58     public void tearDown() throws Exception {
59         SLF4JBridgeHandler.uninstall();
60         log4jRoot.getLoggerRepository().resetConfiguration();
61     }
62 
63     @Test
testSmoke()64     public void testSmoke() {
65         SLF4JBridgeHandler.install();
66 
67         String msg = "msg";
68         julLogger.info(msg);
69         assertEquals(1, listAppender.list.size());
70         LoggingEvent le = (LoggingEvent) listAppender.list.get(0);
71         assertEquals(LOGGER_NAME, le.getLoggerName());
72         assertEquals(msg, le.getMessage());
73 
74         // get the location info in the event.
75         // Note that this must have been computed previously
76         // within an appender for the following assertion to
77         // work properly
78         LocationInfo li = le.getLocationInformation();
79         System.out.println(li.fullInfo);
80         assertEquals("SLF4JBridgeHandlerTest.java", li.getFileName());
81         assertEquals("testSmoke", li.getMethodName());
82     }
83 
84     @Test
LOGBACK_1612()85     public void LOGBACK_1612() {
86         SLF4JBridgeHandler.install();
87 
88         String msg = "LOGBACK_1612";
89         julLogger.finer(msg);
90         assertEquals(1, listAppender.list.size());
91         LoggingEvent le = (LoggingEvent) listAppender.list.get(0);
92         assertEquals(LOGGER_NAME, le.getLoggerName());
93         assertEquals(msg, le.getMessage());
94 
95     }
96 
97     @Test
testLevels()98     public void testLevels() {
99         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
testLogWithResourceBundle()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
testLogWithResourceBundleWithParameters()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
testLogWithPlaceholderNoParameters()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
illFormattedInputShouldBeReturnedAsIs()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
withNullMessage()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 
assertLevel(int index, org.apache.log4j.Level expectedLevel)222     void assertLevel(int index, org.apache.log4j.Level expectedLevel) {
223         LoggingEvent le = listAppender.list.get(index);
224         assertEquals(expectedLevel, le.getLevel());
225     }
226 }
227