From 6748b20863b760682e28ff020cab53e6b5a45650 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Tue, 9 Jul 2019 19:51:12 +0100 Subject: [PATCH] Prevent JUL loggers from being GCed once their level has been set Fixes gh-17217 --- .../boot/logging/java/JavaLoggingSystem.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/java/JavaLoggingSystem.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/java/JavaLoggingSystem.java index a83ab4fc1e7..0b04ee77517 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/java/JavaLoggingSystem.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/java/JavaLoggingSystem.java @@ -21,6 +21,7 @@ import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Collections; import java.util.Enumeration; +import java.util.HashSet; import java.util.List; import java.util.Set; import java.util.logging.Level; @@ -51,6 +52,8 @@ public class JavaLoggingSystem extends AbstractLoggingSystem { private static final LogLevels LEVELS = new LogLevels<>(); + private final Set configuredLoggers = Collections.synchronizedSet(new HashSet<>()); + static { LEVELS.map(LogLevel.TRACE, Level.FINEST); LEVELS.map(LogLevel.DEBUG, Level.FINE); @@ -119,6 +122,7 @@ public class JavaLoggingSystem extends AbstractLoggingSystem { } Logger logger = Logger.getLogger(loggerName); if (logger != null) { + this.configuredLoggers.add(logger); logger.setLevel(LEVELS.convertSystemToNative(level)); } } @@ -159,6 +163,11 @@ public class JavaLoggingSystem extends AbstractLoggingSystem { return new ShutdownHandler(); } + @Override + public void cleanUp() { + this.configuredLoggers.clear(); + } + private final class ShutdownHandler implements Runnable { @Override