From 6ba5329a790224b64acbc005e518436ea52684a5 Mon Sep 17 00:00:00 2001 From: Yubao Liu Date: Mon, 15 Aug 2022 19:29:36 +0800 Subject: [PATCH] Allow different log level for FILE and CONSOLE appender - java.util.logging isn't supported yet See gh-32076 --- .../src/docs/asciidoc/features/logging.adoc | 8 ++++++++ .../boot/logging/LoggingSystemProperties.java | 12 ++++++++++++ .../logging/logback/DefaultLogbackConfiguration.java | 9 +++++++++ .../additional-spring-configuration-metadata.json | 12 ++++++++++++ .../boot/logging/log4j2/log4j2-file.xml | 6 ++++++ .../springframework/boot/logging/log4j2/log4j2.xml | 3 +++ .../boot/logging/logback/console-appender.xml | 3 +++ .../boot/logging/logback/defaults.xml | 2 ++ .../boot/logging/logback/file-appender.xml | 3 +++ 9 files changed, 58 insertions(+) diff --git a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/features/logging.adoc b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/features/logging.adoc index acb6235b1f7..5f36f43f630 100644 --- a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/features/logging.adoc +++ b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/features/logging.adoc @@ -336,6 +336,10 @@ To help with the customization, some other properties are transferred from the S | `CONSOLE_LOG_CHARSET` | The charset to use for console logging. +| configprop:logging.threshold.console[] +| `CONSOLE_LOG_THRESHOLD` +| The log level threshold to use for console logging. + | configprop:logging.pattern.file[] | `FILE_LOG_PATTERN` | The log pattern to use in a file (if `LOG_FILE` is enabled). @@ -344,6 +348,10 @@ To help with the customization, some other properties are transferred from the S | `FILE_LOG_CHARSET` | The charset to use for file logging (if `LOG_FILE` is enabled). +| configprop:logging.threshold.file[] +| `FILE_LOG_THRESHOLD` +| The log level threshold to use for file logging. + | configprop:logging.pattern.level[] | `LOG_LEVEL_PATTERN` | The format to use when rendering the log level (default `%5p`). diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/LoggingSystemProperties.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/LoggingSystemProperties.java index 05a6d6e9171..7852b7e57f1 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/LoggingSystemProperties.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/LoggingSystemProperties.java @@ -70,6 +70,11 @@ public class LoggingSystemProperties { */ public static final String CONSOLE_LOG_CHARSET = "CONSOLE_LOG_CHARSET"; + /** + * The log level threshold for console log. + */ + public static final String CONSOLE_LOG_THRESHOLD = "CONSOLE_LOG_THRESHOLD"; + /** * The name of the System property that contains the file log pattern. */ @@ -80,6 +85,11 @@ public class LoggingSystemProperties { */ public static final String FILE_LOG_CHARSET = "FILE_LOG_CHARSET"; + /** + * The log level threshold for file log. + */ + public static final String FILE_LOG_THRESHOLD = "FILE_LOG_THRESHOLD"; + /** * The name of the System property that contains the log level pattern. */ @@ -139,9 +149,11 @@ public class LoggingSystemProperties { setSystemProperty(PID_KEY, new ApplicationPid().toString()); setSystemProperty(resolver, CONSOLE_LOG_PATTERN, "logging.pattern.console"); setSystemProperty(resolver, CONSOLE_LOG_CHARSET, "logging.charset.console", getDefaultCharset().name()); + setSystemProperty(resolver, CONSOLE_LOG_THRESHOLD, "logging.threshold.console"); setSystemProperty(resolver, LOG_DATEFORMAT_PATTERN, "logging.pattern.dateformat"); setSystemProperty(resolver, FILE_LOG_PATTERN, "logging.pattern.file"); setSystemProperty(resolver, FILE_LOG_CHARSET, "logging.charset.file", getDefaultCharset().name()); + setSystemProperty(resolver, FILE_LOG_THRESHOLD, "logging.threshold.file"); setSystemProperty(resolver, LOG_LEVEL_PATTERN, "logging.pattern.level"); if (logFile != null) { logFile.applyToSystemProperties(); diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/DefaultLogbackConfiguration.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/DefaultLogbackConfiguration.java index 3ed66dd5a65..4ba2cc8759e 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/DefaultLogbackConfiguration.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/DefaultLogbackConfiguration.java @@ -20,6 +20,7 @@ import java.nio.charset.Charset; import ch.qos.logback.classic.Level; import ch.qos.logback.classic.encoder.PatternLayoutEncoder; +import ch.qos.logback.classic.filter.ThresholdFilter; import ch.qos.logback.classic.spi.ILoggingEvent; import ch.qos.logback.core.Appender; import ch.qos.logback.core.ConsoleAppender; @@ -76,11 +77,13 @@ class DefaultLogbackConfiguration { String defaultCharset = Charset.defaultCharset().name(); config.getContext().putProperty("CONSOLE_LOG_CHARSET", resolve(config, "${CONSOLE_LOG_CHARSET:-" + defaultCharset + "}")); + config.getContext().putProperty("CONSOLE_LOG_THRESHOLD", resolve(config, "${CONSOLE_LOG_THRESHOLD:-TRACE}")); config.getContext().putProperty("FILE_LOG_PATTERN", resolve(config, "${FILE_LOG_PATTERN:-" + "%d{${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd'T'HH:mm:ss.SSSXXX}} ${LOG_LEVEL_PATTERN:-%5p} ${PID:- } --- [%t] " + "%-40.40logger{39} : %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}")); config.getContext().putProperty("FILE_LOG_CHARSET", resolve(config, "${FILE_LOG_CHARSET:-" + defaultCharset + "}")); + config.getContext().putProperty("FILE_LOG_THRESHOLD", resolve(config, "${FILE_LOG_THRESHOLD:-TRACE}")); config.logger("org.apache.catalina.startup.DigesterFactory", Level.ERROR); config.logger("org.apache.catalina.util.LifecycleBase", Level.ERROR); config.logger("org.apache.coyote.http11.Http11NioProtocol", Level.WARN); @@ -93,6 +96,9 @@ class DefaultLogbackConfiguration { private Appender consoleAppender(LogbackConfigurator config) { ConsoleAppender appender = new ConsoleAppender<>(); + ThresholdFilter filter = new ThresholdFilter(); + filter.setLevel(resolve(config, "${CONSOLE_LOG_THRESHOLD}")); + appender.addFilter(filter); PatternLayoutEncoder encoder = new PatternLayoutEncoder(); encoder.setPattern(resolve(config, "${CONSOLE_LOG_PATTERN}")); encoder.setCharset(resolveCharset(config, "${CONSOLE_LOG_CHARSET}")); @@ -104,6 +110,9 @@ class DefaultLogbackConfiguration { private Appender fileAppender(LogbackConfigurator config, String logFile) { RollingFileAppender appender = new RollingFileAppender<>(); + ThresholdFilter filter = new ThresholdFilter(); + filter.setLevel(resolve(config, "${FILE_LOG_THRESHOLD}")); + appender.addFilter(filter); PatternLayoutEncoder encoder = new PatternLayoutEncoder(); encoder.setPattern(resolve(config, "${FILE_LOG_PATTERN}")); encoder.setCharset(resolveCharset(config, "${FILE_LOG_CHARSET}")); diff --git a/spring-boot-project/spring-boot/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/spring-boot-project/spring-boot/src/main/resources/META-INF/additional-spring-configuration-metadata.json index 42342c20423..4835aa0c30a 100644 --- a/spring-boot-project/spring-boot/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ b/spring-boot-project/spring-boot/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -203,6 +203,18 @@ "sourceType": "org.springframework.boot.context.logging.LoggingApplicationListener", "defaultValue": true }, + { + "name": "logging.threshold.console", + "type": "java.lang.String", + "description": "Log level threshold for console output.", + "defaultValue": "TRACE" + }, + { + "name": "logging.threshold.file", + "type": "java.lang.String", + "description": "Log level threshold for file output.", + "defaultValue": "TRACE" + }, { "name": "spring.application.index", "type": "java.lang.Integer", diff --git a/spring-boot-project/spring-boot/src/main/resources/org/springframework/boot/logging/log4j2/log4j2-file.xml b/spring-boot-project/spring-boot/src/main/resources/org/springframework/boot/logging/log4j2/log4j2-file.xml index 3f3a79db207..d7c510bb7a9 100644 --- a/spring-boot-project/spring-boot/src/main/resources/org/springframework/boot/logging/log4j2/log4j2-file.xml +++ b/spring-boot-project/spring-boot/src/main/resources/org/springframework/boot/logging/log4j2/log4j2-file.xml @@ -10,9 +10,15 @@ + + + + + + diff --git a/spring-boot-project/spring-boot/src/main/resources/org/springframework/boot/logging/log4j2/log4j2.xml b/spring-boot-project/spring-boot/src/main/resources/org/springframework/boot/logging/log4j2/log4j2.xml index f70d01d28be..818465b116a 100644 --- a/spring-boot-project/spring-boot/src/main/resources/org/springframework/boot/logging/log4j2/log4j2.xml +++ b/spring-boot-project/spring-boot/src/main/resources/org/springframework/boot/logging/log4j2/log4j2.xml @@ -10,6 +10,9 @@ + + + diff --git a/spring-boot-project/spring-boot/src/main/resources/org/springframework/boot/logging/logback/console-appender.xml b/spring-boot-project/spring-boot/src/main/resources/org/springframework/boot/logging/logback/console-appender.xml index d172e4a9ef3..5c11f8b743a 100644 --- a/spring-boot-project/spring-boot/src/main/resources/org/springframework/boot/logging/logback/console-appender.xml +++ b/spring-boot-project/spring-boot/src/main/resources/org/springframework/boot/logging/logback/console-appender.xml @@ -7,6 +7,9 @@ initialization performed by Boot + + ${CONSOLE_LOG_THRESHOLD} + ${CONSOLE_LOG_PATTERN} ${CONSOLE_LOG_CHARSET} diff --git a/spring-boot-project/spring-boot/src/main/resources/org/springframework/boot/logging/logback/defaults.xml b/spring-boot-project/spring-boot/src/main/resources/org/springframework/boot/logging/logback/defaults.xml index ae8b87b273a..bc2ec123819 100644 --- a/spring-boot-project/spring-boot/src/main/resources/org/springframework/boot/logging/logback/defaults.xml +++ b/spring-boot-project/spring-boot/src/main/resources/org/springframework/boot/logging/logback/defaults.xml @@ -11,8 +11,10 @@ Default logback configuration provided for import + + diff --git a/spring-boot-project/spring-boot/src/main/resources/org/springframework/boot/logging/logback/file-appender.xml b/spring-boot-project/spring-boot/src/main/resources/org/springframework/boot/logging/logback/file-appender.xml index 2365a4df04e..26b37be0c9e 100644 --- a/spring-boot-project/spring-boot/src/main/resources/org/springframework/boot/logging/logback/file-appender.xml +++ b/spring-boot-project/spring-boot/src/main/resources/org/springframework/boot/logging/logback/file-appender.xml @@ -7,6 +7,9 @@ initialization performed by Boot + + ${FILE_LOG_THRESHOLD} + ${FILE_LOG_PATTERN} ${FILE_LOG_CHARSET}