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("yay"); 046 047 @Before 048 public void setUp() throws Exception { 049 listAppender.extractLocationInfo = true; 050 log4jRoot = org.apache.log4j.Logger.getRootLogger(); 051 log4jRoot.addAppender(listAppender); 052 log4jRoot.setLevel(org.apache.log4j.Level.TRACE); 053 } 054 055 @After 056 public void tearDown() throws Exception { 057 SLF4JBridgeHandler.uninstall(); 058 log4jRoot.getLoggerRepository().resetConfiguration(); 059 } 060 061 @Test 062 public void testSmoke() { 063 SLF4JBridgeHandler.install(); 064 String msg = "msg"; 065 julLogger.info(msg); 066 assertEquals(1, listAppender.list.size()); 067 LoggingEvent le = (LoggingEvent) listAppender.list.get(0); 068 assertEquals(LOGGER_NAME, le.getLoggerName()); 069 assertEquals(msg, le.getMessage()); 070 071 // get the location info in the event. 072 // Note that this must have been computed previously 073 // within an appender for the following assertion to 074 // work properly 075 LocationInfo li = le.getLocationInformation(); 076 System.out.println(li.fullInfo); 077 assertEquals("SLF4JBridgeHandlerTest.java", li.getFileName()); 078 assertEquals("testSmoke", li.getMethodName()); 079 } 080 081 @Test 082 public void testLevels() { 083 SLF4JBridgeHandler.install(); 084 String msg = "msg"; 085 julLogger.setLevel(Level.ALL); 086 087 julLogger.finest(msg); 088 julLogger.finer(msg); 089 julLogger.fine(msg); 090 julLogger.info(msg); 091 julLogger.warning(msg); 092 julLogger.severe(msg); 093 094 assertEquals(6, listAppender.list.size()); 095 int i = 0; 096 assertLevel(i++, org.apache.log4j.Level.TRACE); 097 assertLevel(i++, org.apache.log4j.Level.DEBUG); 098 assertLevel(i++, org.apache.log4j.Level.DEBUG); 099 assertLevel(i++, org.apache.log4j.Level.INFO); 100 assertLevel(i++, org.apache.log4j.Level.WARN); 101 assertLevel(i++, org.apache.log4j.Level.ERROR); 102 } 103 104 @Test 105 public void testLogWithResourceBundle() { 106 SLF4JBridgeHandler.install(); 107 108 String resourceBundleName = "org.slf4j.bridge.testLogStrings"; 109 ResourceBundle bundle = ResourceBundle.getBundle(resourceBundleName); 110 String resourceKey = "resource_key"; 111 String expectedMsg = bundle.getString(resourceKey); 112 String msg = resourceKey; 113 114 java.util.logging.Logger julResourceBundleLogger = java.util.logging.Logger.getLogger("yay", resourceBundleName); 115 116 julResourceBundleLogger.info(msg); 117 assertEquals(1, listAppender.list.size()); 118 LoggingEvent le = (LoggingEvent) listAppender.list.get(0); 119 assertEquals(LOGGER_NAME, le.getLoggerName()); 120 assertEquals(expectedMsg, le.getMessage()); 121 } 122 123 @Test 124 public void testLogWithResourceBundleWithParameters() { 125 SLF4JBridgeHandler.install(); 126 127 String resourceBundleName = "org.slf4j.bridge.testLogStrings"; 128 ResourceBundle bundle = ResourceBundle.getBundle(resourceBundleName); 129 130 java.util.logging.Logger julResourceBundleLogger = java.util.logging.Logger.getLogger("foo", resourceBundleName); 131 132 String resourceKey1 = "resource_key_1"; 133 String expectedMsg1 = bundle.getString(resourceKey1); 134 julResourceBundleLogger.info(resourceKey1); // 1st log 135 136 String resourceKey2 = "resource_key_2"; 137 Object[] params2 = new Object[] { "foo", "bar" }; 138 String expectedMsg2 = MessageFormat.format(bundle.getString(resourceKey2), params2); 139 julResourceBundleLogger.log(Level.INFO, resourceKey2, params2); // 2nd log 140 141 String resourceKey3 = "invalidKey {0}"; 142 Object[] params3 = new Object[] { "John" }; 143 String expectedMsg3 = MessageFormat.format(resourceKey3, params3); 144 julResourceBundleLogger.log(Level.INFO, resourceKey3, params3); // 3rd log 145 146 julLogger.log(Level.INFO, resourceKey3, params3); // 4th log 147 148 assertEquals(4, listAppender.list.size()); 149 150 LoggingEvent le = null; 151 152 le = (LoggingEvent) listAppender.list.get(0); 153 assertEquals("foo", le.getLoggerName()); 154 assertEquals(expectedMsg1, le.getMessage()); 155 156 le = (LoggingEvent) listAppender.list.get(1); 157 assertEquals("foo", le.getLoggerName()); 158 assertEquals(expectedMsg2, le.getMessage()); 159 160 le = (LoggingEvent) listAppender.list.get(2); 161 assertEquals("foo", le.getLoggerName()); 162 assertEquals(expectedMsg3, le.getMessage()); 163 164 le = (LoggingEvent) listAppender.list.get(3); 165 assertEquals("yay", le.getLoggerName()); 166 assertEquals(expectedMsg3, le.getMessage()); 167 } 168 169 @Test 170 public void testLogWithPlaceholderNoParameters() { 171 SLF4JBridgeHandler.install(); 172 String msg = "msg {non-number-string}"; 173 julLogger.logp(Level.INFO, "SLF4JBridgeHandlerTest", "testLogWithPlaceholderNoParameters", msg, new Object[0]); 174 175 assertEquals(1, listAppender.list.size()); 176 LoggingEvent le = (LoggingEvent) listAppender.list.get(0); 177 assertEquals(LOGGER_NAME, le.getLoggerName()); 178 assertEquals(msg, le.getMessage()); 179 } 180 181 // See http://jira.qos.ch/browse/SLF4J-337 182 183 @Test 184 public void illFormattedInputShouldBeReturnedAsIs() { 185 SLF4JBridgeHandler.install(); 186 String msg = "foo {18=bad} {0}"; 187 188 julLogger.log(Level.INFO, msg, "ignored parameter due to IllegalArgumentException"); 189 assertEquals(1, listAppender.list.size()); 190 LoggingEvent le = (LoggingEvent) listAppender.list.get(0); 191 assertEquals(msg, le.getMessage()); 192 } 193 194 void assertLevel(int index, org.apache.log4j.Level expectedLevel) { 195 LoggingEvent le = (LoggingEvent) listAppender.list.get(index); 196 assertEquals(expectedLevel, le.getLevel()); 197 } 198}