From 844782b20bf1de0ca22b031fd2874a55a483044e Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Mon, 29 Jan 2018 13:11:19 +0000 Subject: [PATCH] Remove default JUL handler to prevent duplicate console logging By default, JUL configures a single root handler. That handler is a ConsoleHandler. Previously, we removed all root handlers from JUL but this is problematic in environments where other handlers are registered with JUL and those handlers need to be retained. 0679d436 attempted to fix the problem by leaving the root handlers in place and only adding and removing the bridge handler. This resulted in log output from Tomcat (and anything else that uses JUL) being duplicated. This commit makes another attempt at tackling the problem. It attempts to detect JUL's default configuration (a single root handler that's a ConsoleHandler) and only removes the handler if it appears to be from the default configuration. For environments where default JUL configuration is being used, this will prevent duplicate logging and for environments where custom JUL configuration is being used, this will prevent that configuration from being undone. Closes gh-8933 --- .../boot/logging/Slf4JLoggingSystem.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/Slf4JLoggingSystem.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/Slf4JLoggingSystem.java index 7ec30e2c33f..22161d2ba26 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/Slf4JLoggingSystem.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/Slf4JLoggingSystem.java @@ -16,6 +16,11 @@ package org.springframework.boot.logging; +import java.util.logging.ConsoleHandler; +import java.util.logging.Handler; +import java.util.logging.LogManager; +import java.util.logging.Logger; + import org.slf4j.bridge.SLF4JBridgeHandler; import org.springframework.util.Assert; @@ -74,6 +79,7 @@ public abstract class Slf4JLoggingSystem extends AbstractLoggingSystem { private void removeJdkLoggingBridgeHandler() { try { if (isBridgeHandlerAvailable()) { + removeDefaultRootHandler(); SLF4JBridgeHandler.uninstall(); } } @@ -82,4 +88,17 @@ public abstract class Slf4JLoggingSystem extends AbstractLoggingSystem { } } + private void removeDefaultRootHandler() { + try { + Logger rootLogger = LogManager.getLogManager().getLogger(""); + Handler[] handlers = rootLogger.getHandlers(); + if (handlers.length == 1 && handlers[0] instanceof ConsoleHandler) { + rootLogger.removeHandler(handlers[0]); + } + } + catch (Throwable ex) { + // Ignore and continue + } + } + }