mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-08-29 03:06:45 +08:00
Ensure that logging system tests do not leave log files open
See gh-17107
This commit is contained in:
parent
0b8247bd62
commit
73cf11535d
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -70,6 +70,7 @@ class JavaLoggingSystemTests extends AbstractLoggingSystemTests {
|
||||
@AfterEach
|
||||
void resetLogger() {
|
||||
this.logger.setLevel(Level.OFF);
|
||||
this.loggingSystem.getShutdownHandler().run();
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user