• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.slf4j.issue;
2 
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertTrue;
5 
6 import java.util.List;
7 import java.util.concurrent.LinkedBlockingQueue;
8 import java.util.logging.Handler;
9 import java.util.logging.Level;
10 import java.util.logging.LogRecord;
11 
12 import org.junit.After;
13 import org.junit.Before;
14 import org.junit.Test;
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
17 import org.slf4j.event.EventConstants;
18 import org.slf4j.event.SubstituteLoggingEvent;
19 import org.slf4j.helpers.SubstituteLogger;
20 import org.slf4j.helpers.SubstituteServiceProvider;
21 import org.slf4j.jul.ListHandler;
22 
23 public class CallerInfoTest {
24     Level oldLevel;
25     java.util.logging.Logger root = java.util.logging.Logger.getLogger("");
26 
27     ListHandler listHandler = new ListHandler();
28 
29     @Before
setUp()30     public void setUp() throws Exception {
31         oldLevel = root.getLevel();
32         root.setLevel(Level.FINE);
33         // removeAllHandlers(root);
34         root.addHandler(listHandler);
35     }
36 
37     @After
tearDown()38     public void tearDown() throws Exception {
39         root.setLevel(oldLevel);
40         removeListHandlers(root);
41     }
42 
removeListHandlers(java.util.logging.Logger logger)43     void removeListHandlers(java.util.logging.Logger logger) {
44         Handler[] handlers = logger.getHandlers();
45         for (Handler h : handlers) {
46             if (h instanceof ListHandler)
47                 logger.removeHandler(h);
48         }
49     }
50 
51     @Test
testCallerInfo()52     public void testCallerInfo() {
53         Logger logger = LoggerFactory.getLogger("bla");
54         logger.debug("hello");
55 
56         List<LogRecord> recordList = listHandler.recordList;
57 
58         assertEquals(1, recordList.size());
59 
60         LogRecord logRecod = recordList.get(0);
61         assertEquals(this.getClass().getName(), logRecod.getSourceClassName());
62     }
63 
64     // Do we preserve location info using fluent API?
65     // See https://jira.qos.ch/browse/SLF4J-511
66 
67     @Test
testCallerInfoWithFluentAPI()68     public void testCallerInfoWithFluentAPI() {
69         Logger logger = LoggerFactory.getLogger("bla");
70         logger.atDebug().log("hello");
71 
72         List<LogRecord> recordList = listHandler.recordList;
73 
74         assertEquals(1, recordList.size());
75 
76         LogRecord logRecod = recordList.get(0);
77         assertEquals(this.getClass().getName(), logRecod.getSourceClassName());
78     }
79 
80     @Test
testPostInitializationCallerInfoWithSubstituteLogger()81     public void testPostInitializationCallerInfoWithSubstituteLogger() {
82         Logger logger = LoggerFactory.getLogger("bla");
83         SubstituteLogger substituteLogger = new SubstituteLogger("bla", null, false);
84         substituteLogger.setDelegate(logger);
85         substituteLogger.debug("hello");
86 
87         List<LogRecord> recordList = listHandler.recordList;
88 
89         assertEquals(1, recordList.size());
90 
91         LogRecord logRecod = recordList.get(0);
92         assertEquals(CallerInfoTest.class.getName(), logRecod.getSourceClassName());
93     }
94 
95     // In this case we KNOW that we CANNOT KNOW the caller
96     @Test
testIntraInitializationCallerInfoWithSubstituteLogger()97     public void testIntraInitializationCallerInfoWithSubstituteLogger() throws InterruptedException {
98         SubstituteServiceProvider substituteServiceProvider = new SubstituteServiceProvider();
99         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 }
123