[logback-dev] svn commit: r1742 - in logback/trunk/logback-classic/src: main/java/ch/qos/logback/classic main/java/ch/qos/logback/classic/util test/java/ch/qos/logback/classic/util
noreply.ceki at qos.ch
noreply.ceki at qos.ch
Thu Aug 7 16:58:04 CEST 2008
Author: ceki
Date: Thu Aug 7 16:58:04 2008
New Revision: 1742
Modified:
logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/LoggerContext.java
logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/util/ContextInitializer.java
logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/util/StatusListenerConfigHelper.java
logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/util/InitializationTest.java
Log:
Relates to LBCLASSIC-59 LBCLASSIC-58
- unit testing addition of status listener by logback.statusListenerClass system property
Modified: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/LoggerContext.java
==============================================================================
--- logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/LoggerContext.java (original)
+++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/LoggerContext.java Thu Aug 7 16:58:04 2008
@@ -31,13 +31,15 @@
import ch.qos.logback.core.spi.FilterReply;
import ch.qos.logback.core.spi.LifeCycle;
import ch.qos.logback.core.status.ErrorStatus;
+import ch.qos.logback.core.status.StatusListener;
+import ch.qos.logback.core.status.StatusManager;
/**
- * LoggerContext glues many of the logback-classic components together. In principle,
- * every logback-classic component instance is attached either directly or indirecty
- * to a LoggerContext instance. Just as importantly LoggerContext implements the
- * {@link ILoggerFactory} acting as the manufacturing source of {@link Logger}
- * instances.
+ * LoggerContext glues many of the logback-classic components together. In
+ * principle, every logback-classic component instance is attached either
+ * directly or indirecty to a LoggerContext instance. Just as importantly
+ * LoggerContext implements the {@link ILoggerFactory} acting as the
+ * manufacturing source of {@link Logger} instances.
*
* @author Ceki Gulcu
*/
@@ -160,7 +162,7 @@
* reference, otherwise returns <code>null</code>.
*
* @param name
- * the name of the logger to search for.
+ * the name of the logger to search for.
*/
public Logger exists(String name) {
return (Logger) loggerCache.get(name);
@@ -180,7 +182,7 @@
Collections.sort(loggerList, new LoggerComparator());
return loggerList;
}
-
+
public LoggerContextRemoteView getLoggerContextRemoteView() {
return loggerContextRemoteView;
}
@@ -189,6 +191,15 @@
root.recursiveReset();
clearAllTurboFilters();
fireOnReset();
+ // TODO is it a good idea to reset the status listeners?
+ resetStatusListeners();
+ }
+
+ void resetStatusListeners() {
+ StatusManager sm = getStatusManager();
+ for (StatusListener sl : sm.getCopyOfStatusListenerList()) {
+ sm.remove(sl);
+ }
}
public void addTurboFilter(TurboFilter newFilter) {
@@ -215,7 +226,7 @@
return cfai.getTurboFilterChainDecision(marker, logger, level, format,
params, t);
}
-
+
final public FilterReply getTurboFilterChainDecision(final Marker marker,
final Logger logger, final Level level, final String format,
final Object param, final Throwable t) {
@@ -223,9 +234,9 @@
return FilterReply.NEUTRAL;
}
return cfai.getTurboFilterChainDecision(marker, logger, level, format,
- new Object[]{param}, t);
+ new Object[] { param }, t);
}
-
+
final public FilterReply getTurboFilterChainDecision(final Marker marker,
final Logger logger, final Level level, final String format,
final Object param1, final Object param2, final Throwable t) {
@@ -233,7 +244,7 @@
return FilterReply.NEUTRAL;
}
return cfai.getTurboFilterChainDecision(marker, logger, level, format,
- new Object[]{param1, param2}, t);
+ new Object[] { param1, param2 }, t);
}
public TurboFilter getFirstTurboFilter() {
Modified: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/util/ContextInitializer.java
==============================================================================
--- logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/util/ContextInitializer.java (original)
+++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/util/ContextInitializer.java Thu Aug 7 16:58:04 2008
@@ -22,7 +22,9 @@
final public static String AUTOCONFIG_FILE = "logback.xml";
final public static String TEST_AUTOCONFIG_FILE = "logback-test.xml";
final public static String CONFIG_FILE_PROPERTY = "logback.configurationFile";
-
+ final public static String STATUS_LISTENER_CLASS = "logback.statusListenerClass";
+ final public static String SYSOUT = "SYSOUT";
+
public static void configureByResource(LoggerContext loggerContext, URL url)
throws JoranException {
if (url == null) {
@@ -80,6 +82,5 @@
ClassLoader tccl = Loader.getTCL();
autoConfig(loggerContext, tccl);
}
-
}
Modified: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/util/StatusListenerConfigHelper.java
==============================================================================
--- logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/util/StatusListenerConfigHelper.java (original)
+++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/util/StatusListenerConfigHelper.java Thu Aug 7 16:58:04 2008
@@ -7,27 +7,23 @@
public class StatusListenerConfigHelper {
- final public static String STATUS_LISTENER_CLASS = "logback.statusListenerClass";
- final public static String SYSOUT = "SYSOUT";
-
-
static void installIfAsked(LoggerContext loggerContext) {
- String slClass = System.getProperty(STATUS_LISTENER_CLASS, null);
+ String slClass = System.getProperty(
+ ContextInitializer.STATUS_LISTENER_CLASS, null);
if (!OptionHelper.isEmpty(slClass)) {
addStatusListener(loggerContext, slClass);
- }
+ }
}
-
+
static void addStatusListener(LoggerContext loggerContext,
String listenerClass) {
StatusListener listener = null;
- if (SYSOUT.equalsIgnoreCase(listenerClass)) {
+ if (ContextInitializer.SYSOUT.equalsIgnoreCase(listenerClass)) {
listener = new OnConsoleStatusListener();
} else {
try {
listener = (StatusListener) OptionHelper.instantiateByClassName(
listenerClass, StatusListener.class, loggerContext);
- loggerContext.getStatusManager().add(listener);
} catch (Exception e) {
// printing on the console is the best we can do
e.printStackTrace();
Modified: logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/util/InitializationTest.java
==============================================================================
--- logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/util/InitializationTest.java (original)
+++ logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/util/InitializationTest.java Thu Aug 7 16:58:04 2008
@@ -1,7 +1,16 @@
package ch.qos.logback.classic.util;
-import junit.framework.TestCase;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.assertEquals;
+
+import java.util.List;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
import org.slf4j.LoggerFactory;
import ch.qos.logback.classic.Logger;
@@ -9,32 +18,38 @@
import ch.qos.logback.core.Appender;
import ch.qos.logback.core.ConsoleAppender;
import ch.qos.logback.core.joran.spi.JoranException;
+import ch.qos.logback.core.status.StatusListener;
+import ch.qos.logback.core.status.TrivialStatusListener;
-public class InitializationTest extends TestCase {
+public class InitializationTest {
org.slf4j.Logger logger = LoggerFactory.getLogger(InitializationTest.class);
LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
Logger root = (Logger) LoggerFactory.getLogger("root");
- protected void setUp() throws Exception {
- super.setUp();
+ @Before
+ public void setUp() throws Exception {
logger.debug("Hello-didily-odily");
-
}
- protected void tearDown() throws Exception {
- super.tearDown();
+ @After
+ public void tearDown() throws Exception {
System.clearProperty(ContextInitializer.CONFIG_FILE_PROPERTY);
+ System.clearProperty(ContextInitializer.STATUS_LISTENER_CLASS);
+ lc.shutdownAndReset();
}
+ @Test
public void testAutoconfig() {
Appender appender = root.getAppender("STDOUT");
assertNotNull(appender);
assertTrue(appender instanceof ConsoleAppender);
}
- public void testReset() {
+ @Test
+ public void testReset() throws JoranException {
{
+ ContextInitializer.autoConfig(lc);
Appender appender = root.getAppender("STDOUT");
assertNotNull(appender);
assertTrue(appender instanceof ConsoleAppender);
@@ -46,17 +61,39 @@
}
}
+ @Test
public void testAutoConfigFromSystemProperties() throws JoranException {
doAutoConfigFromSystemProperties(TeztConstants.TEST_DIR_PREFIX + "input/autoConfig.xml");
doAutoConfigFromSystemProperties("autoConfigAsResource.xml");
// test passing a URL. note the relative path syntax with file:src/test/...
doAutoConfigFromSystemProperties("file:"+TeztConstants.TEST_DIR_PREFIX + "input/autoConfig.xml");
}
+
public void doAutoConfigFromSystemProperties(String val) throws JoranException {
- lc.shutdownAndReset();
+ //lc.shutdownAndReset();
System.setProperty(ContextInitializer.CONFIG_FILE_PROPERTY, val);
ContextInitializer.autoConfig(lc);
Appender appender = root.getAppender("AUTO_BY_SYSTEM_PROPERTY");
assertNotNull(appender);
}
+
+ @Test
+ public void teztAutoStatusListener() throws JoranException {
+ System.setProperty(ContextInitializer.STATUS_LISTENER_CLASS, TrivialStatusListener.class.getName());
+ List<StatusListener> sll = lc.getStatusManager().getCopyOfStatusListenerList();
+ assertEquals(0, sll.size());
+ doAutoConfigFromSystemProperties(TeztConstants.TEST_DIR_PREFIX + "input/autoConfig.xml");
+ sll = lc.getStatusManager().getCopyOfStatusListenerList();
+ assertTrue(sll.size() +" should be 1", sll.size() == 1);
+ }
+
+ @Test
+ public void teztAutoOnConsoleStatusListener() throws JoranException {
+ System.setProperty(ContextInitializer.STATUS_LISTENER_CLASS, ContextInitializer.SYSOUT);
+ List<StatusListener> sll = lc.getStatusManager().getCopyOfStatusListenerList();
+ assertEquals(0, sll.size());
+ doAutoConfigFromSystemProperties(TeztConstants.TEST_DIR_PREFIX + "input/autoConfig.xml");
+ sll = lc.getStatusManager().getCopyOfStatusListenerList();
+ assertTrue(sll.size() +" should be 1", sll.size() == 1);
+ }
}
More information about the logback-dev
mailing list