Commit f7e8e155 authored by Andy Wilkinson's avatar Andy Wilkinson

Shut down logging system as part of clean up processing

Previously, when a LoggingSystem was cleaned up the underlying logging
system (LogBack, Log4J, Log4J2, JUL) was not being properly shut down.
LogBack explicitly recommends stopping it cleanly [1]. While the other
logging systems are less explicity about it, they would all appear to
benefit from being stopped.

This commit updates all four LoggingSystems to stop/reset/clean up to
underlying logging system when cleanUp is called.

Closes gh-4026

[1] http://logback.qos.ch/manual/configuration.html#stopContext
parent 1e50d988
......@@ -113,4 +113,10 @@ public class JavaLoggingSystem extends AbstractLoggingSystem {
logger.setLevel(LEVELS.get(level));
}
@Override
public void cleanUp() {
super.cleanUp();
LogManager.getLogManager().reset();
}
}
......@@ -115,4 +115,10 @@ public class Log4JLoggingSystem extends Slf4JLoggingSystem {
logger.setLevel(LEVELS.get(level));
}
@Override
public void cleanUp() {
super.cleanUp();
LogManager.shutdown();
}
}
......@@ -152,6 +152,12 @@ public class Log4J2LoggingSystem extends Slf4JLoggingSystem {
loadConfiguration(location, logFile);
}
@Override
public void cleanUp() {
super.cleanUp();
getLoggerContext().stop();
}
protected void loadConfiguration(String location, LogFile logFile) {
Assert.notNull(location, "Location must not be null");
if (logFile != null) {
......
......@@ -177,6 +177,7 @@ public class LogbackLoggingSystem extends Slf4JLoggingSystem {
public void cleanUp() {
super.cleanUp();
getLoggerContext().getStatusManager().clear();
getLoggerContext().stop();
}
@Override
......
......@@ -20,6 +20,9 @@ import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.util.Locale;
import java.util.logging.Handler;
import java.util.logging.LogManager;
import java.util.logging.LogRecord;
import org.apache.commons.logging.impl.Jdk14Logger;
import org.junit.After;
......@@ -34,6 +37,7 @@ import org.springframework.util.StringUtils;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
......@@ -160,4 +164,33 @@ public class JavaLoggingSystemTests extends AbstractLoggingSystemTests {
equalTo(1));
}
@Test
public void cleanUpResetsLogManager() throws Exception {
this.loggingSystem.beforeInitialize();
this.loggingSystem.initialize(null, null, null);
this.logger.getLogger().addHandler(new NoOpHandler());
assertThat(this.logger.getLogger().getHandlers().length, is(equalTo(1)));
LogManager.getLogManager().reset();
assertThat(this.logger.getLogger().getHandlers().length, is(equalTo(0)));
}
private static final class NoOpHandler extends Handler {
@Override
public void publish(LogRecord record) {
}
@Override
public void flush() {
}
@Override
public void close() throws SecurityException {
}
}
}
......@@ -139,6 +139,17 @@ public class Log4JLoggingSystemTests extends AbstractLoggingSystemTests {
assertFalse(bridgeHandlerInstalled());
}
@Test
public void cleanUpStopsLogManager() {
this.loggingSystem.beforeInitialize();
this.loggingSystem.initialize(null, null, null);
assertTrue(org.apache.log4j.LogManager.getLoggerRepository().getRootLogger()
.getAllAppenders().hasMoreElements());
this.loggingSystem.cleanUp();
assertFalse(org.apache.log4j.LogManager.getLoggerRepository().getRootLogger()
.getAllAppenders().hasMoreElements());
}
private boolean bridgeHandlerInstalled() {
java.util.logging.Logger rootLogger = LogManager.getLogManager().getLogger("");
Handler[] handlers = rootLogger.getHandlers();
......
......@@ -24,6 +24,7 @@ import java.util.List;
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.FileConfigurationMonitor;
import org.hamcrest.Matcher;
......@@ -238,6 +239,17 @@ public class Log4J2LoggingSystemTests extends AbstractLoggingSystemTests {
}
}
@Test
public void cleanupStopsContext() throws Exception {
this.loggingSystem.beforeInitialize();
this.logger.info("Hidden");
this.loggingSystem.initialize(null, null, null);
LoggerContext context = (LoggerContext) LogManager.getContext(false);
assertFalse(context.isStopped());
this.loggingSystem.cleanUp();
assertTrue(context.isStopped());
}
private static class TestLog4J2LoggingSystem extends Log4J2LoggingSystem {
private List<String> availableClasses = new ArrayList<String>();
......@@ -247,8 +259,7 @@ public class Log4J2LoggingSystemTests extends AbstractLoggingSystemTests {
}
public Configuration getConfiguration() {
return ((org.apache.logging.log4j.core.LoggerContext) LogManager
.getContext(false)).getConfiguration();
return ((LoggerContext) LogManager.getContext(false)).getConfiguration();
}
@Override
......
......@@ -51,6 +51,7 @@ import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
......@@ -121,6 +122,7 @@ public class LogbackLoggingSystemTests extends AbstractLoggingSystemTests {
@Test
public void testBasicConfigLocation() throws Exception {
this.loggingSystem.beforeInitialize();
this.loggingSystem.initialize(this.initializationContext, null, null);
ILoggerFactory factory = StaticLoggerBinder.getSingleton().getLoggerFactory();
LoggerContext context = (LoggerContext) factory;
Logger root = context.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME);
......@@ -305,6 +307,19 @@ public class LogbackLoggingSystemTests extends AbstractLoggingSystemTests {
}
}
@Test
public void cleanUpStopsContext() throws Exception {
this.loggingSystem.beforeInitialize();
this.loggingSystem.initialize(this.initializationContext, null, null);
ILoggerFactory factory = StaticLoggerBinder.getSingleton().getLoggerFactory();
LoggerContext context = (LoggerContext) factory;
Logger root = context.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME);
assertNotNull(root.getAppender("CONSOLE"));
this.loggingSystem.cleanUp();
assertNull(root.getAppender("CONSOLE"));
}
private String getLineWithText(File file, String outputSearch) throws Exception {
return getLineWithText(FileCopyUtils.copyToString(new FileReader(file)),
outputSearch);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment