001package org.slf4j.issue; 002 003import static org.junit.Assert.assertEquals; 004import static org.junit.Assert.assertTrue; 005 006import java.util.List; 007import java.util.concurrent.LinkedBlockingQueue; 008import java.util.logging.Handler; 009import java.util.logging.Level; 010import java.util.logging.LogRecord; 011 012import org.junit.After; 013import org.junit.Before; 014import org.junit.Test; 015import org.slf4j.Logger; 016import org.slf4j.LoggerFactory; 017import org.slf4j.event.EventConstants; 018import org.slf4j.event.SubstituteLoggingEvent; 019import org.slf4j.helpers.SubstituteLogger; 020import org.slf4j.helpers.SubstituteServiceProvider; 021import org.slf4j.jul.ListHandler; 022 023public class CallerInfoTest { 024 Level oldLevel; 025 java.util.logging.Logger root = java.util.logging.Logger.getLogger(""); 026 027 ListHandler listHandler = new ListHandler(); 028 029 @Before 030 public void setUp() throws Exception { 031 oldLevel = root.getLevel(); 032 root.setLevel(Level.FINE); 033 // removeAllHandlers(root); 034 root.addHandler(listHandler); 035 } 036 037 @After 038 public void tearDown() throws Exception { 039 root.setLevel(oldLevel); 040 removeListHandlers(root); 041 } 042 043 void removeListHandlers(java.util.logging.Logger logger) { 044 Handler[] handlers = logger.getHandlers(); 045 for (Handler h : handlers) { 046 if (h instanceof ListHandler) 047 logger.removeHandler(h); 048 } 049 } 050 051 @Test 052 public void testCallerInfo() { 053 Logger logger = LoggerFactory.getLogger("bla"); 054 logger.debug("hello"); 055 056 List<LogRecord> recordList = listHandler.recordList; 057 058 assertEquals(1, recordList.size()); 059 060 LogRecord logRecod = recordList.get(0); 061 assertEquals(this.getClass().getName(), logRecod.getSourceClassName()); 062 } 063 064 // Do we preserve location info using fluent API? 065 // See https://jira.qos.ch/browse/SLF4J-511 066 067 @Test 068 public void testCallerInfoWithFluentAPI() { 069 Logger logger = LoggerFactory.getLogger("bla"); 070 logger.atDebug().log("hello"); 071 072 List<LogRecord> recordList = listHandler.recordList; 073 074 assertEquals(1, recordList.size()); 075 076 LogRecord logRecod = recordList.get(0); 077 assertEquals(this.getClass().getName(), logRecod.getSourceClassName()); 078 } 079 080 @Test 081 public void testPostInitializationCallerInfoWithSubstituteLogger() { 082 Logger logger = LoggerFactory.getLogger("bla"); 083 SubstituteLogger substituteLogger = new SubstituteLogger("bla", null, false); 084 substituteLogger.setDelegate(logger); 085 substituteLogger.debug("hello"); 086 087 List<LogRecord> recordList = listHandler.recordList; 088 089 assertEquals(1, recordList.size()); 090 091 LogRecord logRecod = recordList.get(0); 092 assertEquals(CallerInfoTest.class.getName(), logRecod.getSourceClassName()); 093 } 094 095 // In this case we KNOW that we CANNOT KNOW the caller 096 @Test 097 public void testIntraInitializationCallerInfoWithSubstituteLogger() throws InterruptedException { 098 SubstituteServiceProvider substituteServiceProvider = new SubstituteServiceProvider(); 099 String loggerName = "bkla"; 100 substituteServiceProvider.getLoggerFactory().getLogger(loggerName); 101 SubstituteLogger substituteLogger = substituteServiceProvider.getSubstituteLoggerFactory().getLoggers().get(0); 102 assertEquals(loggerName, substituteLogger.getName()); 103 104 substituteLogger.debug("jello"); 105 Logger logger = LoggerFactory.getLogger(loggerName); 106 substituteLogger.setDelegate(logger); 107 108 final LinkedBlockingQueue<SubstituteLoggingEvent> queue = substituteServiceProvider.getSubstituteLoggerFactory().getEventQueue(); 109 110 SubstituteLoggingEvent substituteLoggingEvent = queue.take(); 111 assertTrue(substituteLogger.isDelegateEventAware()); 112 substituteLogger.log(substituteLoggingEvent); 113 114 List<LogRecord> recordList = listHandler.recordList; 115 116 assertEquals(1, recordList.size()); 117 118 LogRecord logRecod = recordList.get(0); 119 assertEquals(EventConstants.NA_SUBST, logRecod.getSourceClassName()); 120 } 121 122}