Ensure that logging system tests do not leave log files open

See gh-17107
This commit is contained in:
Andy Wilkinson 2019-07-02 12:57:46 +01:00
parent 0b8247bd62
commit 73cf11535d
3 changed files with 18 additions and 10 deletions

View File

@ -28,10 +28,10 @@ import java.util.Properties;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
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.apache.logging.log4j.core.Logger;
import org.assertj.core.api.Condition;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
@ -61,6 +61,7 @@ import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.test.context.support.TestPropertySourceUtils;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.util.StringUtils;
import static org.assertj.core.api.Assertions.assertThat;
@ -495,17 +496,15 @@ class ConfigFileApplicationListenerTests {
}
private void withDebugLogging(Runnable runnable) {
LoggerContext loggingContext = (LoggerContext) LogManager.getContext(false);
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();
Log log = LogFactory.getLog(ConfigFileApplicationListener.class);
Logger logger = (Logger) ReflectionTestUtils.getField(log, "logger");
Level previousLevel = logger.getLevel();
logger.setLevel(Level.DEBUG);
try {
runnable.run();
}
finally {
configuration.removeLogger(ConfigFileApplicationListener.class.getName());
loggingContext.updateLoggers();
logger.setLevel(previousLevel);
}
}

View File

@ -70,6 +70,7 @@ class JavaLoggingSystemTests extends AbstractLoggingSystemTests {
@AfterEach
void resetLogger() {
this.logger.setLevel(Level.OFF);
this.loggingSystem.getShutdownHandler().run();
}
@AfterEach

View File

@ -29,6 +29,7 @@ import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.Reconfigurable;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@ -66,8 +67,12 @@ class Log4J2LoggingSystemTests extends AbstractLoggingSystemTests {
private Logger logger;
private Configuration configuration;
@BeforeEach
void setup() {
LoggerContext loggerContext = (LoggerContext) LogManager.getContext(false);
this.configuration = loggerContext.getConfiguration();
this.loggingSystem.cleanUp();
this.logger = LogManager.getLogger(getClass());
}
@ -75,6 +80,9 @@ class Log4J2LoggingSystemTests extends AbstractLoggingSystemTests {
@AfterEach
void cleanUp() {
this.loggingSystem.cleanUp();
LoggerContext loggerContext = (LoggerContext) LogManager.getContext(false);
loggerContext.stop();
loggerContext.start(((Reconfigurable) this.configuration).reconfigure());
}
@Test