1 33 34 package org.slf4j.impl; 35 36 import java.util.logging.Handler ; 37 import java.util.logging.LogRecord ; 38 import java.util.logging.Logger ; 39 40 import junit.framework.TestCase; 41 42 public class JDK14AdapterLoggerNameTest extends TestCase { 43 private MockHandler mockHandler; 44 45 protected void setUp() throws Exception { 46 super.setUp(); 47 Logger logger = Logger.getLogger("TEST"); 48 mockHandler = new MockHandler(); 49 removeHandlers(logger); 50 logger.addHandler(mockHandler); 51 } 52 53 protected void tearDown() throws Exception { 54 removeHandlers(Logger.getLogger("TEST")); 55 super.tearDown(); 56 } 57 58 public void testLoggerNameusingJdkLogging() throws Exception { 59 Logger.getLogger("TEST").info("test message"); 60 assertCorrectLoggerName(); 61 62 } 63 64 public void testLoggerNameUsingSlf4j() throws Exception { 65 JDK14LoggerFactory factory = new JDK14LoggerFactory(); 66 org.slf4j.Logger logger = factory.getLogger("TEST"); 67 logger.info("test message"); 68 assertCorrectLoggerName(); 69 } 70 71 private void removeHandlers(Logger logger) { 72 logger.setUseParentHandlers(false); 73 Handler [] handlers = logger.getHandlers(); 74 for (int i = 0; i < handlers.length; i++) { 75 logger.removeHandler(handlers[i]); 76 } 77 } 78 79 private void assertCorrectLoggerName() { 80 assertNotNull("no log record", mockHandler.record); 81 assertNotNull("missing logger name", mockHandler.record.getLoggerName()); 82 } 83 84 private class MockHandler extends java.util.logging.Handler { 85 public LogRecord record; 86 87 public void close() throws SecurityException { 88 } 89 90 public void flush() { 91 } 92 93 public void publish(LogRecord record) { 94 this.record = record; 95 } 96 97 } 98 } | Popular Tags |