Skip to content

Commit

Permalink
Merge pull request #38 from halvards-master
Browse files Browse the repository at this point in the history
Throw exception for unexpected file extension for config
  • Loading branch information
halvards authored and tony19 committed Jan 21, 2014
2 parents 14fc8bb + ac09805 commit 6d2ae9f
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.gaffer.GafferUtil;
import ch.qos.logback.classic.joran.JoranConfigurator;
import ch.qos.logback.core.LogbackException;
import ch.qos.logback.core.joran.spi.JoranException;
import ch.qos.logback.core.status.ErrorStatus;
import ch.qos.logback.core.status.InfoStatus;
Expand Down Expand Up @@ -68,11 +69,12 @@ public void configureByResource(URL url) throws JoranException {
sm.add(new ErrorStatus("Groovy classes are not available on the class path. ABORTING INITIALIZATION.",
loggerContext));
}
}
if (url.toString().endsWith("xml")) {
} else if (url.toString().endsWith("xml")) {
JoranConfigurator configurator = new JoranConfigurator();
configurator.setContext(loggerContext);
configurator.doConfigure(url);
} else {
throw new LogbackException("Unexpected filename extension of file [" + url.toString() + "]. Should be either .groovy or .xml");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

import org.junit.After;
Expand All @@ -34,10 +37,13 @@
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.Appender;
import ch.qos.logback.core.ConsoleAppender;
import ch.qos.logback.core.CoreConstants;
import ch.qos.logback.core.LogbackException;
import ch.qos.logback.core.joran.spi.JoranException;
import ch.qos.logback.core.status.StatusListener;
import ch.qos.logback.core.status.TrivialStatusListener;
import sun.security.jca.ProviderList;
import ch.qos.logback.core.util.Loader;

public class ContextInitializerTest {

Expand Down Expand Up @@ -112,4 +118,42 @@ public void autoOnConsoleStatusListener() throws JoranException {
sll = loggerContext.getStatusManager().getCopyOfStatusListenerList();
assertTrue(sll.size() +" should be 1", sll.size() == 1);
}

@Test
public void shouldConfigureFromXmlFile() throws MalformedURLException, JoranException {
LoggerContext loggerContext = new LoggerContext();
ContextInitializer initializer = new ContextInitializer(loggerContext);
assertNull(loggerContext.getObject(CoreConstants.SAFE_JORAN_CONFIGURATION));

URL configurationFileUrl = Loader.getResource("BOO_logback-test.xml", Thread.currentThread().getContextClassLoader());
initializer.configureByResource(configurationFileUrl);

assertNotNull(loggerContext.getObject(CoreConstants.SAFE_JORAN_CONFIGURATION));
}

@Test
public void shouldConfigureFromGroovyScript() throws MalformedURLException, JoranException {
LoggerContext loggerContext = new LoggerContext();
ContextInitializer initializer = new ContextInitializer(loggerContext);
assertNull(loggerContext.getObject(CoreConstants.CONFIGURATION_WATCH_LIST));

URL configurationFileUrl = Loader.getResource("test.groovy", Thread.currentThread().getContextClassLoader());
initializer.configureByResource(configurationFileUrl);

assertNotNull(loggerContext.getObject(CoreConstants.CONFIGURATION_WATCH_LIST));
}

@Test
public void shouldThrowExceptionIfUnexpectedConfigurationFileExtension() throws JoranException {
LoggerContext loggerContext = new LoggerContext();
ContextInitializer initializer = new ContextInitializer(loggerContext);

URL configurationFileUrl = Loader.getResource("README.txt", Thread.currentThread().getContextClassLoader());
try {
initializer.configureByResource(configurationFileUrl);
fail("Should throw LogbackException");
} catch (LogbackException expectedException) {
// pass
}
}
}
15 changes: 15 additions & 0 deletions logback-classic/src/test/resources/test.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import ch.qos.logback.core.ConsoleAppender
import ch.qos.logback.core.encoder.LayoutWrappingEncoder
import ch.qos.logback.classic.PatternLayout
import ch.qos.logback.classic.Level
import ch.qos.logback.core.status.OnConsoleStatusListener
import ch.qos.logback.classic.Logger

appender("C", ConsoleAppender) {
encoder(LayoutWrappingEncoder) {
layout(PatternLayout) {
pattern = "%m%n"
}
}
}
root Level.WARN, ["C"]

0 comments on commit 6d2ae9f

Please sign in to comment.