Skip to content

Commit

Permalink
Use DefaultJoranConfigurator instead of copy/pasting configureByResource
Browse files Browse the repository at this point in the history
Signed-off-by: Matthias Kurz <m.kurz@irregular.at>
  • Loading branch information
mkurz authored and ceki committed Aug 12, 2023
1 parent b240892 commit 9ce9d1c
Showing 1 changed file with 6 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import ch.qos.logback.classic.ClassicTestConstants;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.joran.JoranConfigurator;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.Appender;
import ch.qos.logback.core.ConsoleAppender;
Expand Down Expand Up @@ -161,7 +160,9 @@ public void shouldConfigureFromXmlFile() throws MalformedURLException, JoranExce

URL configurationFileUrl = Loader.getResource("BOO_logback-test.xml",
Thread.currentThread().getContextClassLoader());
configureByResource(configurationFileUrl);
DefaultJoranConfigurator joranConfigurator = new DefaultJoranConfigurator();
joranConfigurator.setContext(loggerContext);
joranConfigurator.configureByResource(configurationFileUrl);

assertNotNull(loggerContext.getObject(CoreConstants.SAFE_JORAN_CONFIGURATION));
}
Expand All @@ -178,25 +179,13 @@ public void shouldConfigureFromXmlFile() throws MalformedURLException, JoranExce
// assertNotNull(loggerContext.getObject(CoreConstants.CONFIGURATION_WATCH_LIST));
// }

private void configureByResource(URL url) throws JoranException {
if (url == null) {
throw new IllegalArgumentException("URL argument cannot be null");
}
final String urlString = url.toString();
if (urlString.endsWith("xml")) {
JoranConfigurator configurator = new JoranConfigurator();
configurator.setContext(loggerContext);
configurator.doConfigure(url);
} else {
throw new LogbackException("Unexpected filename extension of file [" + url + "]. Should be .xml");
}
}

@Test
public void shouldThrowExceptionIfUnexpectedConfigurationFileExtension() throws JoranException {
URL configurationFileUrl = Loader.getResource("README.txt", Thread.currentThread().getContextClassLoader());
try {
this.configureByResource(configurationFileUrl);
DefaultJoranConfigurator joranConfigurator = new DefaultJoranConfigurator();
joranConfigurator.setContext(loggerContext);
joranConfigurator.configureByResource(configurationFileUrl);
fail("Should throw LogbackException");
} catch (LogbackException expectedException) {
// pass
Expand Down

0 comments on commit 9ce9d1c

Please sign in to comment.