From c3430d5883f8a13168a4341bd27ad6b0015e47cc Mon Sep 17 00:00:00 2001 From: Dmytro Nosan Date: Fri, 22 Feb 2019 14:27:01 +0200 Subject: [PATCH 1/2] Debug mode is not logging web and sql related loggers See gh-16018 --- .../logging/LoggingApplicationListener.java | 14 +++++++------- .../LoggingApplicationListenerTests.java | 17 ++++++++++++++++- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/logging/LoggingApplicationListener.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/logging/LoggingApplicationListener.java index ac2854b9dd7..8ba14af1be8 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/logging/LoggingApplicationListener.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/logging/LoggingApplicationListener.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2018 the original author or authors. + * Copyright 2012-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,6 +21,7 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Locale; import java.util.Map; +import java.util.Optional; import java.util.concurrent.atomic.AtomicBoolean; import org.apache.commons.logging.Log; @@ -320,12 +321,11 @@ public class LoggingApplicationListener implements GenericApplicationListener { } protected void initializeLogLevel(LoggingSystem system, LogLevel level) { - List loggers = LOG_LEVEL_LOGGERS.get(level); - if (loggers != null) { - for (String logger : loggers) { - system.setLogLevel(logger, level); - } - } + Optional.ofNullable(LOG_LEVEL_LOGGERS.get(level)).orElse(Collections.emptyList()) + .stream() + .flatMap((logger) -> DEFAULT_GROUP_LOGGERS + .getOrDefault(logger, Collections.singletonList(logger)).stream()) + .forEach((logger) -> system.setLogLevel(logger, level)); } protected void setLogLevels(LoggingSystem system, Environment environment) { diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/logging/LoggingApplicationListenerTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/logging/LoggingApplicationListenerTests.java index 8f5cafa3ac4..03627fb4786 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/logging/LoggingApplicationListenerTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/logging/LoggingApplicationListenerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2018 the original author or authors. + * Copyright 2012-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -262,6 +262,21 @@ public class LoggingApplicationListenerTests { assertThat(this.outputCapture.toString()).doesNotContain("testattrace"); } + @Test + public void parseDebugArgExpandGroups() { + addPropertiesToEnvironment(this.context, "debug"); + this.initializer.initialize(this.context.getEnvironment(), + this.context.getClassLoader()); + ch.qos.logback.classic.Logger sqlGroup = this.loggerContext + .getLogger("org.hibernate.SQL"); + ch.qos.logback.classic.Logger webGroup = this.loggerContext + .getLogger("org.springframework.boot.actuate.endpoint.web"); + webGroup.debug("testdebugwebgroup"); + sqlGroup.debug("testdebugsqlgroup"); + assertThat(this.outputCapture.toString()).contains("testdebugwebgroup"); + assertThat(this.outputCapture.toString()).contains("testdebugsqlgroup"); + } + @Test public void parseTraceArg() { TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context, "trace"); From 978f80122b9138f5606bd9e33597807b90129d61 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Wed, 27 Feb 2019 13:37:18 +0100 Subject: [PATCH 2/2] Polish "Debug mode is not logging web and sql related loggers" Closes gh-16018 --- .../context/logging/LoggingApplicationListener.java | 4 +--- .../logging/LoggingApplicationListenerTests.java | 11 ++++------- 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/logging/LoggingApplicationListener.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/logging/LoggingApplicationListener.java index 8ba14af1be8..e9415ac11ae 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/logging/LoggingApplicationListener.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/logging/LoggingApplicationListener.java @@ -21,7 +21,6 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Locale; import java.util.Map; -import java.util.Optional; import java.util.concurrent.atomic.AtomicBoolean; import org.apache.commons.logging.Log; @@ -321,8 +320,7 @@ public class LoggingApplicationListener implements GenericApplicationListener { } protected void initializeLogLevel(LoggingSystem system, LogLevel level) { - Optional.ofNullable(LOG_LEVEL_LOGGERS.get(level)).orElse(Collections.emptyList()) - .stream() + LOG_LEVEL_LOGGERS.getOrDefault(level, Collections.emptyList()).stream() .flatMap((logger) -> DEFAULT_GROUP_LOGGERS .getOrDefault(logger, Collections.singletonList(logger)).stream()) .forEach((logger) -> system.setLogLevel(logger, level)); diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/logging/LoggingApplicationListenerTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/logging/LoggingApplicationListenerTests.java index 03627fb4786..b0a4d711679 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/logging/LoggingApplicationListenerTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/logging/LoggingApplicationListenerTests.java @@ -264,15 +264,12 @@ public class LoggingApplicationListenerTests { @Test public void parseDebugArgExpandGroups() { - addPropertiesToEnvironment(this.context, "debug"); + TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context, "debug"); this.initializer.initialize(this.context.getEnvironment(), this.context.getClassLoader()); - ch.qos.logback.classic.Logger sqlGroup = this.loggerContext - .getLogger("org.hibernate.SQL"); - ch.qos.logback.classic.Logger webGroup = this.loggerContext - .getLogger("org.springframework.boot.actuate.endpoint.web"); - webGroup.debug("testdebugwebgroup"); - sqlGroup.debug("testdebugsqlgroup"); + this.logFactory.getInstance("org.springframework.boot.actuate.endpoint.web") + .debug("testdebugwebgroup"); + this.logFactory.getInstance("org.hibernate.SQL").debug("testdebugsqlgroup"); assertThat(this.outputCapture.toString()).contains("testdebugwebgroup"); assertThat(this.outputCapture.toString()).contains("testdebugsqlgroup"); }