diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/logging/AutoConfigurationReportLoggingInitializerTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/logging/AutoConfigurationReportLoggingInitializerTests.java index a10d6e5b73c..2a94e345c07 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/logging/AutoConfigurationReportLoggingInitializerTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/logging/AutoConfigurationReportLoggingInitializerTests.java @@ -16,20 +16,14 @@ package org.springframework.boot.autoconfigure.logging; -import java.util.ArrayList; import java.util.Arrays; -import java.util.List; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogConfigurationException; -import org.apache.commons.logging.LogFactory; -import org.apache.commons.logging.impl.LogFactoryImpl; -import org.apache.commons.logging.impl.NoOpLog; -import org.junit.After; -import org.junit.Before; + +import ch.qos.logback.classic.Level; +import ch.qos.logback.classic.Logger; +import ch.qos.logback.classic.LoggerContext; +import org.junit.Rule; import org.junit.Test; -import org.mockito.invocation.InvocationOnMock; -import org.mockito.stubbing.Answer; +import org.slf4j.impl.StaticLoggerBinder; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.condition.ConditionEvaluationReport; @@ -37,6 +31,7 @@ import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoCon import org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration; import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration; import org.springframework.boot.context.event.ApplicationFailedEvent; +import org.springframework.boot.testutil.InternalOutputCapture; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -47,10 +42,6 @@ import org.springframework.web.context.support.AnnotationConfigWebApplicationCon import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.fail; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.BDDMockito.given; -import static org.mockito.BDDMockito.willAnswer; -import static org.mockito.Mockito.mock; /** * Tests for {@link AutoConfigurationReportLoggingInitializer}. @@ -60,57 +51,10 @@ import static org.mockito.Mockito.mock; */ public class AutoConfigurationReportLoggingInitializerTests { - private static ThreadLocal logThreadLocal = new ThreadLocal<>(); - - private Log log; - - private AutoConfigurationReportLoggingInitializer initializer; - - protected List debugLog = new ArrayList<>(); - - protected List infoLog = new ArrayList<>(); - - @Before - public void setup() { - setupLogging(true, true); - } - - private void setupLogging(boolean debug, boolean info) { - this.log = mock(Log.class); - logThreadLocal.set(this.log); - - given(this.log.isDebugEnabled()).willReturn(debug); - willAnswer(new Answer() { - - @Override - public Object answer(InvocationOnMock invocation) throws Throwable { - return AutoConfigurationReportLoggingInitializerTests.this.debugLog - .add(String.valueOf(invocation.getArguments()[0])); - } - - }).given(this.log).debug(any()); - - given(this.log.isInfoEnabled()).willReturn(info); - willAnswer(new Answer() { + @Rule + public InternalOutputCapture outputCapture = new InternalOutputCapture(); - @Override - public Object answer(InvocationOnMock invocation) throws Throwable { - return AutoConfigurationReportLoggingInitializerTests.this.infoLog - .add(String.valueOf(invocation.getArguments()[0])); - } - - }).given(this.log).info(any()); - - LogFactory.releaseAll(); - System.setProperty(LogFactory.FACTORY_PROPERTY, MockLogFactory.class.getName()); - this.initializer = new AutoConfigurationReportLoggingInitializer(); - } - - @After - public void cleanup() { - System.clearProperty(LogFactory.FACTORY_PROPERTY); - LogFactory.releaseAll(); - } + private AutoConfigurationReportLoggingInitializer initializer = new AutoConfigurationReportLoggingInitializer(); @Test public void logsDebugOnContextRefresh() { @@ -118,8 +62,10 @@ public class AutoConfigurationReportLoggingInitializerTests { this.initializer.initialize(context); context.register(Config.class); context.refresh(); - this.initializer.onApplicationEvent(new ContextRefreshedEvent(context)); - assertThat(this.debugLog.size()).isNotEqualTo(0); + withDebugLogging(() -> { + this.initializer.onApplicationEvent(new ContextRefreshedEvent(context)); + }); + assertThat(this.outputCapture.toString()).contains("AUTO-CONFIGURATION REPORT"); } @Test @@ -132,16 +78,16 @@ public class AutoConfigurationReportLoggingInitializerTests { fail("Did not error"); } catch (Exception ex) { - this.initializer.onApplicationEvent(new ApplicationFailedEvent( - new SpringApplication(), new String[0], context, ex)); + withDebugLogging(() -> { + this.initializer.onApplicationEvent(new ApplicationFailedEvent( + new SpringApplication(), new String[0], context, ex)); + }); } - assertThat(this.debugLog.size()).isNotEqualTo(0); - assertThat(this.infoLog.size()).isEqualTo(0); + assertThat(this.outputCapture.toString()).contains("AUTO-CONFIGURATION REPORT"); } @Test public void logsInfoOnErrorIfDebugDisabled() { - setupLogging(false, true); AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); this.initializer.initialize(context); context.register(ErrorConfig.class); @@ -153,8 +99,9 @@ public class AutoConfigurationReportLoggingInitializerTests { this.initializer.onApplicationEvent(new ApplicationFailedEvent( new SpringApplication(), new String[0], context, ex)); } - assertThat(this.debugLog.size()).isEqualTo(0); - assertThat(this.infoLog.size()).isNotEqualTo(0); + assertThat(this.outputCapture.toString()).contains("Error starting" + + " ApplicationContext. To display the auto-configuration report re-run" + + " your application with 'debug' enabled."); } @Test @@ -165,13 +112,10 @@ public class AutoConfigurationReportLoggingInitializerTests { ConditionEvaluationReport.get(context.getBeanFactory()) .recordExclusions(Arrays.asList("com.foo.Bar")); context.refresh(); - this.initializer.onApplicationEvent(new ContextRefreshedEvent(context)); - for (String message : this.debugLog) { - System.out.println(message); - } - // Just basic sanity check, test is for visual inspection - String l = this.debugLog.get(0); - assertThat(l) + withDebugLogging(() -> { + this.initializer.onApplicationEvent(new ContextRefreshedEvent(context)); + }); + assertThat(this.outputCapture.toString()) .contains("not a servlet web application (OnWebApplicationCondition)"); } @@ -199,20 +143,23 @@ public class AutoConfigurationReportLoggingInitializerTests { this.initializer .onApplicationEvent(new ApplicationFailedEvent(new SpringApplication(), new String[0], null, new RuntimeException("Planned"))); - assertThat(this.infoLog.get(0)) + assertThat(this.outputCapture.toString()) .contains("Unable to provide auto-configuration report"); } - public static class MockLogFactory extends LogFactoryImpl { - - @Override - public Log getInstance(String name) throws LogConfigurationException { - if (AutoConfigurationReportLoggingInitializer.class.getName().equals(name)) { - return logThreadLocal.get(); - } - return new NoOpLog(); + private void withDebugLogging(Runnable runnable) { + LoggerContext context = (LoggerContext) StaticLoggerBinder.getSingleton() + .getLoggerFactory(); + Logger logger = context + .getLogger(AutoConfigurationReportLoggingInitializer.class); + Level currentLevel = logger.getLevel(); + logger.setLevel(Level.DEBUG); + try { + runnable.run(); + } + finally { + logger.setLevel(currentLevel); } - } @Configuration diff --git a/spring-boot-cli/src/test/java/org/springframework/boot/cli/CliTester.java b/spring-boot-cli/src/test/java/org/springframework/boot/cli/CliTester.java index 5e299a7c1ed..b3de1913dd5 100644 --- a/spring-boot-cli/src/test/java/org/springframework/boot/cli/CliTester.java +++ b/spring-boot-cli/src/test/java/org/springframework/boot/cli/CliTester.java @@ -155,8 +155,10 @@ public class CliTester implements TestRule { return sources; } - public String getOutput() { - return this.outputCapture.toString(); + private String getOutput() { + String output = this.outputCapture.toString(); + this.outputCapture.reset(); + return output; } @Override diff --git a/spring-boot-cli/src/test/java/org/springframework/boot/cli/DirectorySourcesIntegrationTests.java b/spring-boot-cli/src/test/java/org/springframework/boot/cli/DirectorySourcesIntegrationTests.java index ade61351c74..d1dc1a5551c 100644 --- a/spring-boot-cli/src/test/java/org/springframework/boot/cli/DirectorySourcesIntegrationTests.java +++ b/spring-boot-cli/src/test/java/org/springframework/boot/cli/DirectorySourcesIntegrationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2016 the original author or authors. + * Copyright 2012-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -33,20 +33,17 @@ public class DirectorySourcesIntegrationTests { @Test public void runDirectory() throws Exception { - this.cli.run("code"); - assertThat(this.cli.getOutput()).contains("Hello World"); + assertThat(this.cli.run("code")).contains("Hello World"); } @Test public void runDirectoryRecursive() throws Exception { - this.cli.run(""); - assertThat(this.cli.getOutput()).contains("Hello World"); + assertThat(this.cli.run("")).contains("Hello World"); } @Test public void runPathPattern() throws Exception { - this.cli.run("**/*.groovy"); - assertThat(this.cli.getOutput()).contains("Hello World"); + assertThat(this.cli.run("**/*.groovy")).contains("Hello World"); } } diff --git a/spring-boot-cli/src/test/java/org/springframework/boot/cli/ReproIntegrationTests.java b/spring-boot-cli/src/test/java/org/springframework/boot/cli/ReproIntegrationTests.java index ed9f1affa61..033e552c617 100644 --- a/spring-boot-cli/src/test/java/org/springframework/boot/cli/ReproIntegrationTests.java +++ b/spring-boot-cli/src/test/java/org/springframework/boot/cli/ReproIntegrationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2016 the original author or authors. + * Copyright 2012-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -47,14 +47,12 @@ public class ReproIntegrationTests { // this will fail @Test public void securityDependencies() throws Exception { - this.cli.run("secure.groovy"); - assertThat(this.cli.getOutput()).contains("Hello World"); + assertThat(this.cli.run("secure.groovy")).contains("Hello World"); } @Test public void dataJpaDependencies() throws Exception { - this.cli.run("data-jpa.groovy"); - assertThat(this.cli.getOutput()).contains("Hello World"); + assertThat(this.cli.run("data-jpa.groovy")).contains("Hello World"); } @Test diff --git a/spring-boot-cli/src/test/java/org/springframework/boot/cli/RunCommandIntegrationTests.java b/spring-boot-cli/src/test/java/org/springframework/boot/cli/RunCommandIntegrationTests.java index 1fd023e4623..fad5c714cda 100644 --- a/spring-boot-cli/src/test/java/org/springframework/boot/cli/RunCommandIntegrationTests.java +++ b/spring-boot-cli/src/test/java/org/springframework/boot/cli/RunCommandIntegrationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2016 the original author or authors. + * Copyright 2012-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -59,6 +59,7 @@ public class RunCommandIntegrationTests { @Test public void quietModeSuppressesAllCliOutput() throws Exception { + this.cli.run("quiet.groovy"); String output = this.cli.run("quiet.groovy", "-q"); assertThat(output).isEqualTo("Ssshh"); } diff --git a/spring-boot-cli/src/test/java/org/springframework/boot/cli/SampleIntegrationTests.java b/spring-boot-cli/src/test/java/org/springframework/boot/cli/SampleIntegrationTests.java index 5c1ba80052d..7f1ea6641eb 100644 --- a/spring-boot-cli/src/test/java/org/springframework/boot/cli/SampleIntegrationTests.java +++ b/spring-boot-cli/src/test/java/org/springframework/boot/cli/SampleIntegrationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2016 the original author or authors. + * Copyright 2012-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -159,8 +159,7 @@ public class SampleIntegrationTests { @Test public void caching() throws Exception { - this.cli.run("caching.groovy"); - assertThat(this.cli.getOutput()).contains("Hello World"); + assertThat(this.cli.run("caching.groovy")).contains("Hello World"); } } diff --git a/spring-boot-cli/src/test/java/org/springframework/boot/cli/command/install/GroovyGrabDependencyResolverTests.java b/spring-boot-cli/src/test/java/org/springframework/boot/cli/command/install/GroovyGrabDependencyResolverTests.java index 177bb89546d..613612c0c9a 100644 --- a/spring-boot-cli/src/test/java/org/springframework/boot/cli/command/install/GroovyGrabDependencyResolverTests.java +++ b/spring-boot-cli/src/test/java/org/springframework/boot/cli/command/install/GroovyGrabDependencyResolverTests.java @@ -105,10 +105,10 @@ public class GroovyGrabDependencyResolverTests { @Test @SuppressWarnings({ "unchecked", "rawtypes" }) public void resolveShorthandArtifactWithDependencies() throws Exception { - List resolved = this.resolver.resolve(Arrays.asList("spring-core")); + List resolved = this.resolver.resolve(Arrays.asList("spring-beans")); assertThat(resolved).hasSize(2); - assertThat(getNames(resolved)).has((Condition) Matched.by( - hasItems(startsWith("commons-logging-"), startsWith("spring-core-")))); + assertThat(getNames(resolved)).has((Condition) Matched + .by(hasItems(startsWith("spring-core-"), startsWith("spring-beans-")))); } @Test diff --git a/spring-boot-cli/src/test/java/org/springframework/boot/cli/compiler/grape/AetherGrapeEngineTests.java b/spring-boot-cli/src/test/java/org/springframework/boot/cli/compiler/grape/AetherGrapeEngineTests.java index 37c30ff9377..048b0d04aed 100644 --- a/spring-boot-cli/src/test/java/org/springframework/boot/cli/compiler/grape/AetherGrapeEngineTests.java +++ b/spring-boot-cli/src/test/java/org/springframework/boot/cli/compiler/grape/AetherGrapeEngineTests.java @@ -66,8 +66,8 @@ public class AetherGrapeEngineTests { public void dependencyResolution() { Map args = new HashMap<>(); createGrapeEngine(this.springMilestones).grab(args, - createDependency("org.springframework", "spring-jdbc", "3.2.4.RELEASE")); - assertThat(this.groovyClassLoader.getURLs()).hasSize(5); + createDependency("org.springframework", "spring-jdbc", null)); + assertThat(this.groovyClassLoader.getURLs()).hasSize(4); } @Test @@ -153,10 +153,10 @@ public class AetherGrapeEngineTests { args.put("classLoader", customClassLoader); createGrapeEngine(this.springMilestones).grab(args, - createDependency("org.springframework", "spring-jdbc", "3.2.4.RELEASE")); + createDependency("org.springframework", "spring-jdbc", null)); assertThat(this.groovyClassLoader.getURLs().length).isEqualTo(0); - assertThat(customClassLoader.getURLs().length).isEqualTo(5); + assertThat(customClassLoader.getURLs().length).isEqualTo(4); } @Test diff --git a/spring-boot-starters/spring-boot-starter-data-cassandra/pom.xml b/spring-boot-starters/spring-boot-starter-data-cassandra/pom.xml index 2a3f36b20eb..d949164ff59 100644 --- a/spring-boot-starters/spring-boot-starter-data-cassandra/pom.xml +++ b/spring-boot-starters/spring-boot-starter-data-cassandra/pom.xml @@ -30,6 +30,12 @@ org.springframework.data spring-data-cassandra + + + org.slf4j + jcl-over-slf4j + + diff --git a/spring-boot-starters/spring-boot-starter-data-couchbase/pom.xml b/spring-boot-starters/spring-boot-starter-data-couchbase/pom.xml index 0dc9bf36335..2d6c825129e 100644 --- a/spring-boot-starters/spring-boot-starter-data-couchbase/pom.xml +++ b/spring-boot-starters/spring-boot-starter-data-couchbase/pom.xml @@ -26,6 +26,12 @@ org.springframework.data spring-data-couchbase + + + org.slf4j + jcl-over-slf4j + + diff --git a/spring-boot-starters/spring-boot-starter-data-elasticsearch/pom.xml b/spring-boot-starters/spring-boot-starter-data-elasticsearch/pom.xml index 2f0b3aa1579..753493710a8 100644 --- a/spring-boot-starters/spring-boot-starter-data-elasticsearch/pom.xml +++ b/spring-boot-starters/spring-boot-starter-data-elasticsearch/pom.xml @@ -26,6 +26,12 @@ org.springframework.data spring-data-elasticsearch + + + org.slf4j + jcl-over-slf4j + + diff --git a/spring-boot-starters/spring-boot-starter-data-jpa/pom.xml b/spring-boot-starters/spring-boot-starter-data-jpa/pom.xml index 6e3e4164d30..7c5e857c862 100644 --- a/spring-boot-starters/spring-boot-starter-data-jpa/pom.xml +++ b/spring-boot-starters/spring-boot-starter-data-jpa/pom.xml @@ -54,6 +54,10 @@ org.aspectj aspectjrt + + org.slf4j + jcl-over-slf4j + diff --git a/spring-boot-starters/spring-boot-starter-data-ldap/pom.xml b/spring-boot-starters/spring-boot-starter-data-ldap/pom.xml index 6bd40e5e424..0cae3e0081e 100644 --- a/spring-boot-starters/spring-boot-starter-data-ldap/pom.xml +++ b/spring-boot-starters/spring-boot-starter-data-ldap/pom.xml @@ -26,6 +26,12 @@ org.springframework.data spring-data-ldap + + + org.slf4j + jcl-over-slf4j + + diff --git a/spring-boot-starters/spring-boot-starter-data-mongodb-reactive/pom.xml b/spring-boot-starters/spring-boot-starter-data-mongodb-reactive/pom.xml index 258f03e19bb..59cba3e1147 100644 --- a/spring-boot-starters/spring-boot-starter-data-mongodb-reactive/pom.xml +++ b/spring-boot-starters/spring-boot-starter-data-mongodb-reactive/pom.xml @@ -31,6 +31,10 @@ org.mongodb mongo-java-driver + + org.slf4j + jcl-over-slf4j + diff --git a/spring-boot-starters/spring-boot-starter-data-mongodb/pom.xml b/spring-boot-starters/spring-boot-starter-data-mongodb/pom.xml index 53907065ee1..806634cb017 100644 --- a/spring-boot-starters/spring-boot-starter-data-mongodb/pom.xml +++ b/spring-boot-starters/spring-boot-starter-data-mongodb/pom.xml @@ -35,6 +35,10 @@ org.mongodb mongo-java-driver + + org.slf4j + jcl-over-slf4j + diff --git a/spring-boot-starters/spring-boot-starter-data-neo4j/pom.xml b/spring-boot-starters/spring-boot-starter-data-neo4j/pom.xml index 252b4d67985..bdae018fe8c 100644 --- a/spring-boot-starters/spring-boot-starter-data-neo4j/pom.xml +++ b/spring-boot-starters/spring-boot-starter-data-neo4j/pom.xml @@ -25,6 +25,12 @@ org.springframework.data spring-data-neo4j + + + org.slf4j + jcl-over-slf4j + + diff --git a/spring-boot-starters/spring-boot-starter-data-redis/pom.xml b/spring-boot-starters/spring-boot-starter-data-redis/pom.xml index b4b46ad5107..dbfaade3264 100644 --- a/spring-boot-starters/spring-boot-starter-data-redis/pom.xml +++ b/spring-boot-starters/spring-boot-starter-data-redis/pom.xml @@ -26,6 +26,12 @@ org.springframework.data spring-data-redis + + + org.slf4j + jcl-over-slf4j + + redis.clients diff --git a/spring-boot-starters/spring-boot-starter-data-rest/pom.xml b/spring-boot-starters/spring-boot-starter-data-rest/pom.xml index 489c40046cb..ec3b11ba781 100644 --- a/spring-boot-starters/spring-boot-starter-data-rest/pom.xml +++ b/spring-boot-starters/spring-boot-starter-data-rest/pom.xml @@ -38,6 +38,12 @@ org.springframework.data spring-data-rest-webmvc + + + org.slf4j + jcl-over-slf4j + + diff --git a/spring-boot-starters/spring-boot-starter-data-solr/pom.xml b/spring-boot-starters/spring-boot-starter-data-solr/pom.xml index 005fdfeff5b..a7ced6c7b9a 100644 --- a/spring-boot-starters/spring-boot-starter-data-solr/pom.xml +++ b/spring-boot-starters/spring-boot-starter-data-solr/pom.xml @@ -31,11 +31,21 @@ log4j log4j + + org.slf4j + jcl-over-slf4j + org.springframework.data spring-data-solr + + + org.slf4j + jcl-over-slf4j + + org.apache.httpcomponents diff --git a/spring-boot-starters/spring-boot-starter-log4j2/pom.xml b/spring-boot-starters/spring-boot-starter-log4j2/pom.xml index 1cca103623c..267938ff989 100644 --- a/spring-boot-starters/spring-boot-starter-log4j2/pom.xml +++ b/spring-boot-starters/spring-boot-starter-log4j2/pom.xml @@ -31,10 +31,6 @@ org.apache.logging.log4j log4j-core - - org.slf4j - jcl-over-slf4j - org.slf4j jul-to-slf4j diff --git a/spring-boot-starters/spring-boot-starter-logging/pom.xml b/spring-boot-starters/spring-boot-starter-logging/pom.xml index fe8bc31cc53..a451fe6a6c1 100644 --- a/spring-boot-starters/spring-boot-starter-logging/pom.xml +++ b/spring-boot-starters/spring-boot-starter-logging/pom.xml @@ -22,10 +22,6 @@ ch.qos.logback logback-classic - - org.slf4j - jcl-over-slf4j - org.slf4j jul-to-slf4j diff --git a/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java b/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java index ac230eddc79..719262bf5cb 100644 --- a/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java @@ -238,7 +238,7 @@ public class SpringApplicationTests { application.setWebApplicationType(WebApplicationType.NONE); this.context = application.run("--spring.main.banner-mode=log"); verify(application, atLeastOnce()).setBannerMode(Banner.Mode.LOG); - assertThat(this.output.toString()).contains("o.s.boot.SpringApplication"); + assertThat(this.output.toString()).contains("o.s.b.SpringApplication"); } @Test diff --git a/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigFileApplicationListenerTests.java b/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigFileApplicationListenerTests.java index 36e447b9ed9..e4131119ff6 100644 --- a/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigFileApplicationListenerTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigFileApplicationListenerTests.java @@ -28,16 +28,15 @@ import java.util.List; import java.util.Map; import java.util.Properties; -import ch.qos.logback.classic.BasicConfigurator; -import ch.qos.logback.classic.Logger; -import ch.qos.logback.classic.LoggerContext; +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.core.LoggerContext; +import org.apache.logging.log4j.core.config.LoggerConfig; import org.assertj.core.api.Condition; import org.junit.After; -import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; -import org.slf4j.LoggerFactory; import org.springframework.boot.SpringApplication; import org.springframework.boot.WebApplicationType; @@ -90,14 +89,6 @@ public class ConfigFileApplicationListenerTests { private ConfigurableApplicationContext context; - @Before - public void resetLogging() { - LoggerContext loggerContext = ((Logger) LoggerFactory.getLogger(getClass())) - .getLoggerContext(); - loggerContext.reset(); - new BasicConfigurator().configure(loggerContext); - } - @After public void cleanUp() { if (this.context != null) { @@ -442,7 +433,9 @@ public class ConfigFileApplicationListenerTests { ApplicationPreparedEvent event = new ApplicationPreparedEvent( new SpringApplication(), new String[0], new AnnotationConfigApplicationContext()); - this.initializer.onApplicationEvent(event); + withDebugLogging(() -> { + this.initializer.onApplicationEvent(event); + }); String log = this.out.toString(); // First make sure that each profile got processed only once @@ -463,6 +456,23 @@ public class ConfigFileApplicationListenerTests { } } + private void withDebugLogging(Runnable runnable) { + LoggerContext loggingContext = (LoggerContext) LogManager.getContext(true); + org.apache.logging.log4j.core.config.Configuration configuration = loggingContext + .getConfiguration(); + configuration.addLogger(ConfigFileApplicationListener.class.getName(), + new LoggerConfig(ConfigFileApplicationListener.class.getName(), + Level.DEBUG, true)); + loggingContext.updateLoggers(); + try { + runnable.run(); + } + finally { + configuration.removeLogger(ConfigFileApplicationListener.class.getName()); + loggingContext.updateLoggers(); + } + } + private String createLogForProfile(String profile) { String suffix = profile != null ? "-" + profile : ""; String string = ".properties)"; diff --git a/spring-boot/src/test/java/org/springframework/boot/context/logging/LoggingApplicationListenerTests.java b/spring-boot/src/test/java/org/springframework/boot/context/logging/LoggingApplicationListenerTests.java index aa60edf786a..5e6222c6151 100644 --- a/spring-boot/src/test/java/org/springframework/boot/context/logging/LoggingApplicationListenerTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/context/logging/LoggingApplicationListenerTests.java @@ -34,12 +34,15 @@ import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; import org.junit.rules.TemporaryFolder; +import org.junit.runner.RunWith; import org.slf4j.bridge.SLF4JBridgeHandler; import org.springframework.boot.ApplicationPid; import org.springframework.boot.SpringApplication; import org.springframework.boot.context.event.ApplicationFailedEvent; import org.springframework.boot.context.event.ApplicationStartingEvent; +import org.springframework.boot.junit.runner.classpath.ClassPathExclusions; +import org.springframework.boot.junit.runner.classpath.ModifiedClassPathRunner; import org.springframework.boot.logging.AbstractLoggingSystem; import org.springframework.boot.logging.LogFile; import org.springframework.boot.logging.LogLevel; @@ -61,7 +64,7 @@ import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.not; /** - * Tests for {@link LoggingApplicationListener}. + * Tests for {@link LoggingApplicationListener} with Logback. * * @author Dave Syer * @author Phillip Webb @@ -69,6 +72,9 @@ import static org.hamcrest.Matchers.not; * @author Stephane Nicoll * @author Ben Hale */ + +@RunWith(ModifiedClassPathRunner.class) +@ClassPathExclusions("log4j*.jar") public class LoggingApplicationListenerTests { private static final String[] NO_ARGS = {}; diff --git a/spring-boot/src/test/java/org/springframework/boot/logging/java/JavaLoggingSystemTests.java b/spring-boot/src/test/java/org/springframework/boot/logging/java/JavaLoggingSystemTests.java index 9fe1f8dcdf3..fa715308b61 100644 --- a/spring-boot/src/test/java/org/springframework/boot/logging/java/JavaLoggingSystemTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/logging/java/JavaLoggingSystemTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2016 the original author or authors. + * Copyright 2012-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,8 +23,8 @@ import java.util.EnumSet; import java.util.List; import java.util.Locale; import java.util.logging.Level; +import java.util.logging.Logger; -import org.apache.commons.logging.impl.Jdk14Logger; import org.junit.After; import org.junit.Before; import org.junit.Rule; @@ -64,7 +64,7 @@ public class JavaLoggingSystemTests extends AbstractLoggingSystemTests { @Rule public InternalOutputCapture output = new InternalOutputCapture(); - private Jdk14Logger logger; + private Logger logger; private Locale defaultLocale; @@ -72,7 +72,7 @@ public class JavaLoggingSystemTests extends AbstractLoggingSystemTests { public void init() throws SecurityException, IOException { this.defaultLocale = Locale.getDefault(); Locale.setDefault(Locale.ENGLISH); - this.logger = new Jdk14Logger(getClass().getName()); + this.logger = Logger.getLogger(getClass().getName()); } @After @@ -82,7 +82,7 @@ public class JavaLoggingSystemTests extends AbstractLoggingSystemTests { @After public void resetLogger() { - this.logger.getLogger().setLevel(Level.OFF); + this.logger.setLevel(Level.OFF); } @Test @@ -161,9 +161,9 @@ public class JavaLoggingSystemTests extends AbstractLoggingSystemTests { public void setLevel() throws Exception { this.loggingSystem.beforeInitialize(); this.loggingSystem.initialize(null, null, null); - this.logger.debug("Hello"); + this.logger.fine("Hello"); this.loggingSystem.setLogLevel("org.springframework.boot", LogLevel.DEBUG); - this.logger.debug("Hello"); + this.logger.fine("Hello"); assertThat(StringUtils.countOccurrencesOf(this.output.toString(), "Hello")) .isEqualTo(1); } diff --git a/spring-boot/src/test/resources/log4j2-test.xml b/spring-boot/src/test/resources/log4j2-test.xml new file mode 100644 index 00000000000..8c0f7f46525 --- /dev/null +++ b/spring-boot/src/test/resources/log4j2-test.xml @@ -0,0 +1,17 @@ + + + + %xwEx + %5p + + + + + + + + + + + +