Skip to content

Commit a76e84a

Browse files
committed
Restore LoggingSystems’ previous cleanup behaviour
This commit reverts the changes made for gh-4026. Those changes updated each LoggingSystem to close/stop the underlying logging system as part of the clean up processing. Unfortunately, this approach doesn’t work in an environment where their are multiple application contexts and some have a shorter lifecycle than the “main” application context. In such an environment, closing an application context with a shorter lifecycle prior to the main application context being closed will close/stop the main application context’s logging system as, rather than being scoped to an application context, a logging system is shared across multiple application contexts. (The exact details of how widely shared the logging system is varies between logging systems and, in the case of Logback and Log4J2, also depends on which ContextSelector implementation is being used.
1 parent e98aac4 commit a76e84a

File tree

8 files changed

+2
-93
lines changed

8 files changed

+2
-93
lines changed

spring-boot/src/main/java/org/springframework/boot/logging/java/JavaLoggingSystem.java

-6
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,4 @@ public void setLogLevel(String loggerName, LogLevel level) {
113113
logger.setLevel(LEVELS.get(level));
114114
}
115115

116-
@Override
117-
public void cleanUp() {
118-
super.cleanUp();
119-
LogManager.getLogManager().reset();
120-
}
121-
122116
}

spring-boot/src/main/java/org/springframework/boot/logging/log4j/Log4JLoggingSystem.java

-6
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,4 @@ public void setLogLevel(String loggerName, LogLevel level) {
115115
logger.setLevel(LEVELS.get(level));
116116
}
117117

118-
@Override
119-
public void cleanUp() {
120-
super.cleanUp();
121-
LogManager.shutdown();
122-
}
123-
124118
}

spring-boot/src/main/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystem.java

-6
Original file line numberDiff line numberDiff line change
@@ -152,12 +152,6 @@ protected void loadConfiguration(LoggingInitializationContext initializationCont
152152
loadConfiguration(location, logFile);
153153
}
154154

155-
@Override
156-
public void cleanUp() {
157-
super.cleanUp();
158-
getLoggerContext().stop();
159-
}
160-
161155
protected void loadConfiguration(String location, LogFile logFile) {
162156
Assert.notNull(location, "Location must not be null");
163157
if (logFile != null) {

spring-boot/src/main/java/org/springframework/boot/logging/logback/LogbackLoggingSystem.java

-3
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
import org.springframework.util.ResourceUtils;
3838
import org.springframework.util.StringUtils;
3939

40-
import ch.qos.logback.classic.BasicConfigurator;
4140
import ch.qos.logback.classic.Level;
4241
import ch.qos.logback.classic.LoggerContext;
4342
import ch.qos.logback.classic.joran.JoranConfigurator;
@@ -178,8 +177,6 @@ private void addLevelChangePropagator(LoggerContext loggerContext) {
178177
public void cleanUp() {
179178
super.cleanUp();
180179
getLoggerContext().getStatusManager().clear();
181-
getLoggerContext().stop();
182-
BasicConfigurator.configure(getLoggerContext());
183180
}
184181

185182
@Override

spring-boot/src/test/java/org/springframework/boot/logging/java/JavaLoggingSystemTests.java

-33
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@
2020
import java.io.FileFilter;
2121
import java.io.IOException;
2222
import java.util.Locale;
23-
import java.util.logging.Handler;
24-
import java.util.logging.LogManager;
25-
import java.util.logging.LogRecord;
2623

2724
import org.apache.commons.logging.impl.Jdk14Logger;
2825
import org.junit.After;
@@ -37,7 +34,6 @@
3734

3835
import static org.hamcrest.Matchers.equalTo;
3936
import static org.hamcrest.Matchers.greaterThan;
40-
import static org.hamcrest.Matchers.is;
4137
import static org.junit.Assert.assertFalse;
4238
import static org.junit.Assert.assertThat;
4339
import static org.junit.Assert.assertTrue;
@@ -164,33 +160,4 @@ public void setLevel() throws Exception {
164160
equalTo(1));
165161
}
166162

167-
@Test
168-
public void cleanUpResetsLogManager() throws Exception {
169-
this.loggingSystem.beforeInitialize();
170-
this.loggingSystem.initialize(null, null, null);
171-
this.logger.getLogger().addHandler(new NoOpHandler());
172-
assertThat(this.logger.getLogger().getHandlers().length, is(equalTo(1)));
173-
LogManager.getLogManager().reset();
174-
assertThat(this.logger.getLogger().getHandlers().length, is(equalTo(0)));
175-
}
176-
177-
private static final class NoOpHandler extends Handler {
178-
179-
@Override
180-
public void publish(LogRecord record) {
181-
182-
}
183-
184-
@Override
185-
public void flush() {
186-
187-
}
188-
189-
@Override
190-
public void close() throws SecurityException {
191-
192-
}
193-
194-
}
195-
196163
}

spring-boot/src/test/java/org/springframework/boot/logging/log4j/Log4JLoggingSystemTests.java

-11
Original file line numberDiff line numberDiff line change
@@ -139,17 +139,6 @@ public void bridgeHandlerLifecycle() {
139139
assertFalse(bridgeHandlerInstalled());
140140
}
141141

142-
@Test
143-
public void cleanUpStopsLogManager() {
144-
this.loggingSystem.beforeInitialize();
145-
this.loggingSystem.initialize(null, null, null);
146-
assertTrue(org.apache.log4j.LogManager.getLoggerRepository().getRootLogger()
147-
.getAllAppenders().hasMoreElements());
148-
this.loggingSystem.cleanUp();
149-
assertFalse(org.apache.log4j.LogManager.getLoggerRepository().getRootLogger()
150-
.getAllAppenders().hasMoreElements());
151-
}
152-
153142
private boolean bridgeHandlerInstalled() {
154143
java.util.logging.Logger rootLogger = LogManager.getLogManager().getLogger("");
155144
Handler[] handlers = rootLogger.getHandlers();

spring-boot/src/test/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystemTests.java

+2-13
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424

2525
import org.apache.logging.log4j.LogManager;
2626
import org.apache.logging.log4j.Logger;
27-
import org.apache.logging.log4j.core.LoggerContext;
2827
import org.apache.logging.log4j.core.config.Configuration;
2928
import org.apache.logging.log4j.core.config.FileConfigurationMonitor;
3029
import org.hamcrest.Matcher;
@@ -239,17 +238,6 @@ public void customExceptionConversionWord() throws Exception {
239238
}
240239
}
241240

242-
@Test
243-
public void cleanupStopsContext() throws Exception {
244-
this.loggingSystem.beforeInitialize();
245-
this.logger.info("Hidden");
246-
this.loggingSystem.initialize(null, null, null);
247-
LoggerContext context = (LoggerContext) LogManager.getContext(false);
248-
assertFalse(context.isStopped());
249-
this.loggingSystem.cleanUp();
250-
assertTrue(context.isStopped());
251-
}
252-
253241
private static class TestLog4J2LoggingSystem extends Log4J2LoggingSystem {
254242

255243
private List<String> availableClasses = new ArrayList<String>();
@@ -259,7 +247,8 @@ private static class TestLog4J2LoggingSystem extends Log4J2LoggingSystem {
259247
}
260248

261249
public Configuration getConfiguration() {
262-
return ((LoggerContext) LogManager.getContext(false)).getConfiguration();
250+
return ((org.apache.logging.log4j.core.LoggerContext) LogManager
251+
.getContext(false)).getConfiguration();
263252
}
264253

265254
@Override

spring-boot/src/test/java/org/springframework/boot/logging/logback/LogbackLoggingSystemTests.java

-15
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@
5151
import static org.junit.Assert.assertEquals;
5252
import static org.junit.Assert.assertFalse;
5353
import static org.junit.Assert.assertNotNull;
54-
import static org.junit.Assert.assertNull;
5554
import static org.junit.Assert.assertThat;
5655
import static org.junit.Assert.assertTrue;
5756

@@ -122,7 +121,6 @@ public void withFile() throws Exception {
122121
@Test
123122
public void testBasicConfigLocation() throws Exception {
124123
this.loggingSystem.beforeInitialize();
125-
this.loggingSystem.initialize(this.initializationContext, null, null);
126124
ILoggerFactory factory = StaticLoggerBinder.getSingleton().getLoggerFactory();
127125
LoggerContext context = (LoggerContext) factory;
128126
Logger root = context.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME);
@@ -307,19 +305,6 @@ public void customExceptionConversionWord() throws Exception {
307305
}
308306
}
309307

310-
@Test
311-
public void cleanUpStopsContext() throws Exception {
312-
this.loggingSystem.beforeInitialize();
313-
this.loggingSystem.initialize(this.initializationContext, null, null);
314-
ILoggerFactory factory = StaticLoggerBinder.getSingleton().getLoggerFactory();
315-
LoggerContext context = (LoggerContext) factory;
316-
Logger root = context.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME);
317-
assertNotNull(root.getAppender("CONSOLE"));
318-
319-
this.loggingSystem.cleanUp();
320-
assertNull(root.getAppender("CONSOLE"));
321-
}
322-
323308
private String getLineWithText(File file, String outputSearch) throws Exception {
324309
return getLineWithText(FileCopyUtils.copyToString(new FileReader(file)),
325310
outputSearch);

0 commit comments

Comments
 (0)