Skip to content

Commit

Permalink
Ensure that logging system tests do not leave log files open
Browse files Browse the repository at this point in the history
  • Loading branch information
wilkinsona committed Jul 2, 2019
1 parent 0b8247b commit 73cf115
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@
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;
Expand Down Expand Up @@ -61,6 +61,7 @@
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;
Expand Down Expand Up @@ -495,17 +496,15 @@ private void validateProfilePreference(CapturedOutput capturedOutput, String...
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ void init() throws SecurityException {
@AfterEach
void resetLogger() {
this.logger.setLevel(Level.OFF);
this.loggingSystem.getShutdownHandler().run();
}

@AfterEach
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
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;
Expand Down Expand Up @@ -66,15 +67,22 @@ 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());
}

@AfterEach
void cleanUp() {
this.loggingSystem.cleanUp();
LoggerContext loggerContext = (LoggerContext) LogManager.getContext(false);
loggerContext.stop();
loggerContext.start(((Reconfigurable) this.configuration).reconfigure());
}

@Test
Expand Down

0 comments on commit 73cf115

Please sign in to comment.