Align with Spring Framework 5's new Commons Logging adapter

Closes gh-8825
This commit is contained in:
Andy Wilkinson
2017-04-06 13:22:53 +01:00
parent 0186fb2e84
commit f0c5372642
26 changed files with 180 additions and 147 deletions

View File

@@ -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

View File

@@ -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)";

View File

@@ -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 = {};

View File

@@ -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);
}

View File

@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="test" packages="">
<Properties>
<Property name="LOG_EXCEPTION_CONVERSION_WORD">%xwEx</Property>
<Property name="LOG_LEVEL_PATTERN">%5p</Property>
</Properties>
<Appenders>
<Console name="STDOUT" target="SYSTEM_OUT" follow="true">
<PatternLayout pattern="%clr{%d{yyyy-MM-dd HH:mm:ss.SSS}}{faint} %clr{${LOG_LEVEL_PATTERN}} %clr{${sys:PID}}{magenta} %clr{---}{faint} %clr{[%15.15t]}{faint} %clr{%-40.40c{1.}}{cyan} %clr{:}{faint} %m%n${sys:LOG_EXCEPTION_CONVERSION_WORD}"/>
</Console>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="STDOUT"/>
</Root>
</Loggers>
</Configuration>