diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/LogFile.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/LogFile.java index b973bc9001..a1a201202e 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/LogFile.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/LogFile.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2020 the original author or authors. + * Copyright 2012-2023 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. @@ -86,13 +86,13 @@ public class LogFile { * @param properties the properties to apply to */ public void applyTo(Properties properties) { - put(properties, LoggingSystemProperties.LOG_PATH, this.path); - put(properties, LoggingSystemProperties.LOG_FILE, toString()); + put(properties, LoggingSystemProperty.LOG_PATH, this.path); + put(properties, LoggingSystemProperty.LOG_FILE, toString()); } - private void put(Properties properties, String key, String value) { + private void put(Properties properties, LoggingSystemProperty property, String value) { if (StringUtils.hasLength(value)) { - properties.put(key, value); + properties.put(property.getEnvironmentVariableName(), value); } } 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 ff4c6c7eeb..456bb010d0 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 @@ -37,68 +37,120 @@ import org.springframework.util.Assert; * @author Robert Thornton * @author EddĂș MelĂ©ndez * @since 2.0.0 + * @see LoggingSystemProperty */ public class LoggingSystemProperties { /** * The name of the System property that contains the process ID. + * @deprecated since 3.2.0 for removal in 3.4.0 in favor of calling + * {@link LoggingSystemProperty#getEnvironmentVariableName()} on + * {@link LoggingSystemProperty#PID} */ - public static final String PID_KEY = "PID"; + @Deprecated(since = "3.2.0", forRemoval = true) + public static final String PID_KEY = LoggingSystemProperty.PID.getEnvironmentVariableName(); /** * The name of the System property that contains the exception conversion word. + * @deprecated since 3.2.0 for removal in 3.4.0 in favor of calling + * {@link LoggingSystemProperty#getEnvironmentVariableName()} on + * {@link LoggingSystemProperty#EXCEPTION_CONVERSION_WORD} */ - public static final String EXCEPTION_CONVERSION_WORD = "LOG_EXCEPTION_CONVERSION_WORD"; + @Deprecated(since = "3.2.0", forRemoval = true) + public static final String EXCEPTION_CONVERSION_WORD = LoggingSystemProperty.EXCEPTION_CONVERSION_WORD + .getEnvironmentVariableName(); /** * The name of the System property that contains the log file. + * @deprecated since 3.2.0 for removal in 3.4.0 in favor of calling + * {@link LoggingSystemProperty#getEnvironmentVariableName()} on + * {@link LoggingSystemProperty#LOG_FILE} */ - public static final String LOG_FILE = "LOG_FILE"; + @Deprecated(since = "3.2.0", forRemoval = true) + public static final String LOG_FILE = LoggingSystemProperty.LOG_FILE.getEnvironmentVariableName(); /** * The name of the System property that contains the log path. + * @deprecated since 3.2.0 for removal in 3.4.0 in favor of calling + * {@link LoggingSystemProperty#getEnvironmentVariableName()} on + * {@link LoggingSystemProperty#LOG_PATH} */ - public static final String LOG_PATH = "LOG_PATH"; + @Deprecated(since = "3.2.0", forRemoval = true) + public static final String LOG_PATH = LoggingSystemProperty.LOG_PATH.getEnvironmentVariableName(); /** * The name of the System property that contains the console log pattern. + * @deprecated since 3.2.0 for removal in 3.4.0 in favor of calling + * {@link LoggingSystemProperty#getEnvironmentVariableName()} on + * {@link LoggingSystemProperty#CONSOLE_PATTERN} */ - public static final String CONSOLE_LOG_PATTERN = "CONSOLE_LOG_PATTERN"; + @Deprecated(since = "3.2.0", forRemoval = true) + public static final String CONSOLE_LOG_PATTERN = LoggingSystemProperty.CONSOLE_PATTERN.getEnvironmentVariableName(); /** * The name of the System property that contains the console log charset. + * @deprecated since 3.2.0 for removal in 3.4.0 in favor of calling + * {@link LoggingSystemProperty#getEnvironmentVariableName()} on + * {@link LoggingSystemProperty#CONSOLE_CHARSET} */ - public static final String CONSOLE_LOG_CHARSET = "CONSOLE_LOG_CHARSET"; + @Deprecated(since = "3.2.0", forRemoval = true) + public static final String CONSOLE_LOG_CHARSET = LoggingSystemProperty.CONSOLE_CHARSET.getEnvironmentVariableName(); /** * The log level threshold for console log. + * @deprecated since 3.2.0 for removal in 3.4.0 in favor of calling + * {@link LoggingSystemProperty#getEnvironmentVariableName()} on + * {@link LoggingSystemProperty#CONSOLE_THRESHOLD} */ - public static final String CONSOLE_LOG_THRESHOLD = "CONSOLE_LOG_THRESHOLD"; + @Deprecated(since = "3.2.0", forRemoval = true) + public static final String CONSOLE_LOG_THRESHOLD = LoggingSystemProperty.CONSOLE_THRESHOLD + .getEnvironmentVariableName(); /** * The name of the System property that contains the file log pattern. + * @deprecated since 3.2.0 for removal in 3.4.0 in favor of calling + * {@link LoggingSystemProperty#getEnvironmentVariableName()} on + * {@link LoggingSystemProperty#FILE_PATTERN} */ - public static final String FILE_LOG_PATTERN = "FILE_LOG_PATTERN"; + @Deprecated(since = "3.2.0", forRemoval = true) + public static final String FILE_LOG_PATTERN = LoggingSystemProperty.FILE_PATTERN.getEnvironmentVariableName(); /** * The name of the System property that contains the file log charset. + * @deprecated since 3.2.0 for removal in 3.4.0 in favor of calling + * {@link LoggingSystemProperty#getEnvironmentVariableName()} on + * {@link LoggingSystemProperty#FILE_CHARSET} */ - public static final String FILE_LOG_CHARSET = "FILE_LOG_CHARSET"; + @Deprecated(since = "3.2.0", forRemoval = true) + public static final String FILE_LOG_CHARSET = LoggingSystemProperty.FILE_CHARSET.getEnvironmentVariableName(); /** * The log level threshold for file log. + * @deprecated since 3.2.0 for removal in 3.4.0 in favor of calling + * {@link LoggingSystemProperty#getEnvironmentVariableName()} on + * {@link LoggingSystemProperty#FILE_THRESHOLD} */ - public static final String FILE_LOG_THRESHOLD = "FILE_LOG_THRESHOLD"; + @Deprecated(since = "3.2.0", forRemoval = true) + public static final String FILE_LOG_THRESHOLD = LoggingSystemProperty.FILE_THRESHOLD.getEnvironmentVariableName(); /** * The name of the System property that contains the log level pattern. + * @deprecated since 3.2.0 for removal in 3.4.0 in favor of calling + * {@link LoggingSystemProperty#getEnvironmentVariableName()} on + * {@link LoggingSystemProperty#LEVEL_PATTERN} */ - public static final String LOG_LEVEL_PATTERN = "LOG_LEVEL_PATTERN"; + @Deprecated(since = "3.2.0", forRemoval = true) + public static final String LOG_LEVEL_PATTERN = LoggingSystemProperty.LEVEL_PATTERN.getEnvironmentVariableName(); /** * The name of the System property that contains the log date-format pattern. + * @deprecated since 3.2.0 for removal in 3.4.0 in favor of calling + * {@link LoggingSystemProperty#getEnvironmentVariableName()} on + * {@link LoggingSystemProperty#DATEFORMAT_PATTERN} */ - public static final String LOG_DATEFORMAT_PATTERN = "LOG_DATEFORMAT_PATTERN"; + @Deprecated(since = "3.2.0", forRemoval = true) + public static final String LOG_DATEFORMAT_PATTERN = LoggingSystemProperty.DATEFORMAT_PATTERN + .getEnvironmentVariableName(); private static final BiConsumer systemPropertySetter = (name, value) -> { if (System.getProperty(name) == null && value != null) { @@ -144,22 +196,6 @@ public class LoggingSystemProperties { apply(logFile, resolver); } - protected void apply(LogFile logFile, PropertyResolver resolver) { - setSystemProperty(resolver, EXCEPTION_CONVERSION_WORD, "logging.exception-conversion-word"); - 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(); - } - } - private PropertyResolver getPropertyResolver() { if (this.environment instanceof ConfigurableEnvironment configurableEnvironment) { PropertySourcesPropertyResolver resolver = new PropertySourcesPropertyResolver( @@ -171,10 +207,59 @@ public class LoggingSystemProperties { return this.environment; } + protected void apply(LogFile logFile, PropertyResolver resolver) { + String defaultCharsetName = getDefaultCharset().name(); + setSystemProperty(LoggingSystemProperty.PID, new ApplicationPid().toString()); + setSystemProperty(LoggingSystemProperty.CONSOLE_CHARSET, resolver, defaultCharsetName); + setSystemProperty(LoggingSystemProperty.FILE_CHARSET, resolver, defaultCharsetName); + setSystemProperty(LoggingSystemProperty.CONSOLE_THRESHOLD, resolver); + setSystemProperty(LoggingSystemProperty.FILE_THRESHOLD, resolver); + setSystemProperty(LoggingSystemProperty.EXCEPTION_CONVERSION_WORD, resolver); + setSystemProperty(LoggingSystemProperty.CONSOLE_PATTERN, resolver); + setSystemProperty(LoggingSystemProperty.FILE_PATTERN, resolver); + setSystemProperty(LoggingSystemProperty.LEVEL_PATTERN, resolver); + setSystemProperty(LoggingSystemProperty.DATEFORMAT_PATTERN, resolver); + if (logFile != null) { + logFile.applyToSystemProperties(); + } + } + + private void setSystemProperty(LoggingSystemProperty property, PropertyResolver resolver) { + setSystemProperty(property, resolver, null); + } + + private void setSystemProperty(LoggingSystemProperty property, PropertyResolver resolver, String defaultValue) { + String value = (property.getApplicationPropertyName() != null) + ? resolver.getProperty(property.getApplicationPropertyName()) : null; + value = (value != null) ? value : defaultValue; + setSystemProperty(property.getEnvironmentVariableName(), value); + } + + private void setSystemProperty(LoggingSystemProperty property, String value) { + setSystemProperty(property.getEnvironmentVariableName(), value); + } + + /** + * Set a system property. + * @param resolver the resolver used to get the property value + * @param systemPropertyName the system property name + * @param propertyName the application property name + * @deprecated since 3.2.0 for removal in 3.4.0 with no replacement + */ + @Deprecated(since = "3.2.0", forRemoval = true) protected final void setSystemProperty(PropertyResolver resolver, String systemPropertyName, String propertyName) { setSystemProperty(resolver, systemPropertyName, propertyName, null); } + /** + * Set a system property. + * @param resolver the resolver used to get the property value + * @param systemPropertyName the system property name + * @param propertyName the application property name + * @param defaultValue the default value if none can be resolved + * @deprecated since 3.2.0 for removal in 3.4.0 with no replacement + */ + @Deprecated(since = "3.2.0", forRemoval = true) protected final void setSystemProperty(PropertyResolver resolver, String systemPropertyName, String propertyName, String defaultValue) { String value = resolver.getProperty(propertyName); @@ -182,6 +267,11 @@ public class LoggingSystemProperties { setSystemProperty(systemPropertyName, value); } + /** + * Set a system property. + * @param name the property name + * @param value the value + */ protected final void setSystemProperty(String name, String value) { this.setter.accept(name, value); } diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/LoggingSystemProperty.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/LoggingSystemProperty.java new file mode 100644 index 0000000000..d6a7314531 --- /dev/null +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/LoggingSystemProperty.java @@ -0,0 +1,113 @@ +/* + * Copyright 2012-2023 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. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.logging; + +/** + * Logging system properties that can later be used by log configuration files. + * + * @author Phillip Webb + * @since 3.2.0 + * @see LoggingSystemProperties + */ +public enum LoggingSystemProperty { + + /** + * Logging system property for the process ID. + */ + PID("PID"), + + /** + * Logging system property for the log file. + */ + LOG_FILE("LOG_FILE"), + + /** + * Logging system property for the log path. + */ + LOG_PATH("LOG_PATH"), + + /** + * Logging system property for the console log charset. + */ + CONSOLE_CHARSET("CONSOLE_LOG_CHARSET", "logging.charset.console"), + + /** + * Logging system property for the file log charset. + */ + FILE_CHARSET("FILE_LOG_CHARSET", "logging.charset.file"), + + /** + * Logging system property for the console log. + */ + CONSOLE_THRESHOLD("CONSOLE_LOG_THRESHOLD", "logging.threshold.console"), + + /** + * Logging system property for the file log. + */ + FILE_THRESHOLD("FILE_LOG_THRESHOLD", "logging.threshold.file"), + + /** + * Logging system property for the exception conversion word. + */ + EXCEPTION_CONVERSION_WORD("LOG_EXCEPTION_CONVERSION_WORD", "logging.exception-conversion-word"), + + /** + * Logging system property for the console log pattern. + */ + CONSOLE_PATTERN("CONSOLE_LOG_PATTERN", "logging.pattern.console"), + + /** + * Logging system property for the file log pattern. + */ + FILE_PATTERN("FILE_LOG_PATTERN", "logging.pattern.file"), + + /** + * Logging system property for the log level pattern. + */ + LEVEL_PATTERN("LOG_LEVEL_PATTERN", "logging.pattern.level"), + + /** + * Logging system property for the date-format pattern. + */ + DATEFORMAT_PATTERN("LOG_DATEFORMAT_PATTERN", "logging.pattern.dateformat"); + + private final String environmentVariableName; + + private final String applicationPropertyName; + + LoggingSystemProperty(String environmentVariableName) { + this(environmentVariableName, null); + } + + LoggingSystemProperty(String environmentVariableName, String applicationPropertyName) { + this.environmentVariableName = environmentVariableName; + this.applicationPropertyName = applicationPropertyName; + } + + /** + * Return the name of environment variable that can be used to access this property. + * @return the environment variable name + */ + public String getEnvironmentVariableName() { + return this.environmentVariableName; + } + + String getApplicationPropertyName() { + return this.applicationPropertyName; + } + +} diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/java/SimpleFormatter.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/java/SimpleFormatter.java index f92e5eae0e..1701a6a03a 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/java/SimpleFormatter.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/java/SimpleFormatter.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2023 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. @@ -22,7 +22,7 @@ import java.util.Date; import java.util.logging.Formatter; import java.util.logging.LogRecord; -import org.springframework.boot.logging.LoggingSystemProperties; +import org.springframework.boot.logging.LoggingSystemProperty; /** * Simple 'Java Logging' {@link Formatter}. @@ -36,7 +36,7 @@ public class SimpleFormatter extends Formatter { private final String format = getOrUseDefault("LOG_FORMAT", DEFAULT_FORMAT); - private final String pid = getOrUseDefault(LoggingSystemProperties.PID_KEY, "????"); + private final String pid = getOrUseDefault(LoggingSystemProperty.PID.getEnvironmentVariableName(), "????"); private final Date date = new Date(); diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/LogbackLoggingSystemProperties.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/LogbackLoggingSystemProperties.java index f9d97937d7..3b7b715e16 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/LogbackLoggingSystemProperties.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/LogbackLoggingSystemProperties.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2022 the original author or authors. + * Copyright 2012-2023 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. @@ -35,6 +35,7 @@ import org.springframework.util.unit.DataSize; * * @author Phillip Webb * @since 2.4.0 + * @see RollingPolicySystemProperty */ public class LogbackLoggingSystemProperties extends LoggingSystemProperties { @@ -44,28 +45,53 @@ public class LogbackLoggingSystemProperties extends LoggingSystemProperties { /** * The name of the System property that contains the rolled-over log file name * pattern. + * @deprecated since 3.2.0 for removal in 3.4.0 in favor of calling + * {@link RollingPolicySystemProperty#getEnvironmentVariableName()} on + * {@link RollingPolicySystemProperty#FILE_NAME_PATTERN} */ - public static final String ROLLINGPOLICY_FILE_NAME_PATTERN = "LOGBACK_ROLLINGPOLICY_FILE_NAME_PATTERN"; + @Deprecated(since = "3.2.0", forRemoval = true) + public static final String ROLLINGPOLICY_FILE_NAME_PATTERN = RollingPolicySystemProperty.FILE_NAME_PATTERN + .getEnvironmentVariableName(); /** * The name of the System property that contains the clean history on start flag. + * @deprecated since 3.2.0 for removal in 3.4.0 in favor of calling + * {@link RollingPolicySystemProperty#getEnvironmentVariableName()} on + * {@link RollingPolicySystemProperty#CLEAN_HISTORY_ON_START} */ - public static final String ROLLINGPOLICY_CLEAN_HISTORY_ON_START = "LOGBACK_ROLLINGPOLICY_CLEAN_HISTORY_ON_START"; + @Deprecated(since = "3.2.0", forRemoval = true) + public static final String ROLLINGPOLICY_CLEAN_HISTORY_ON_START = RollingPolicySystemProperty.CLEAN_HISTORY_ON_START + .getEnvironmentVariableName(); /** * The name of the System property that contains the file log max size. + * @deprecated since 3.2.0 for removal in 3.4.0 in favor of calling + * {@link RollingPolicySystemProperty#getEnvironmentVariableName()} on + * {@link RollingPolicySystemProperty#MAX_FILE_SIZE} */ - public static final String ROLLINGPOLICY_MAX_FILE_SIZE = "LOGBACK_ROLLINGPOLICY_MAX_FILE_SIZE"; + @Deprecated(since = "3.2.0", forRemoval = true) + public static final String ROLLINGPOLICY_MAX_FILE_SIZE = RollingPolicySystemProperty.MAX_FILE_SIZE + .getEnvironmentVariableName(); /** * The name of the System property that contains the file total size cap. + * @deprecated since 3.2.0 for removal in 3.4.0 in favor of calling + * {@link RollingPolicySystemProperty#getEnvironmentVariableName()} on + * {@link RollingPolicySystemProperty#TOTAL_SIZE_CAP} */ - public static final String ROLLINGPOLICY_TOTAL_SIZE_CAP = "LOGBACK_ROLLINGPOLICY_TOTAL_SIZE_CAP"; + @Deprecated(since = "3.2.0", forRemoval = true) + public static final String ROLLINGPOLICY_TOTAL_SIZE_CAP = RollingPolicySystemProperty.TOTAL_SIZE_CAP + .getEnvironmentVariableName(); /** * The name of the System property that contains the file log max history. + * @deprecated since 3.2.0 for removal in 3.4.0 in favor of calling + * {@link RollingPolicySystemProperty#getEnvironmentVariableName()} on + * {@link RollingPolicySystemProperty#MAX_HISTORY} */ - public static final String ROLLINGPOLICY_MAX_HISTORY = "LOGBACK_ROLLINGPOLICY_MAX_HISTORY"; + @Deprecated(since = "3.2.0", forRemoval = true) + public static final String ROLLINGPOLICY_MAX_HISTORY = RollingPolicySystemProperty.MAX_HISTORY + .getEnvironmentVariableName(); public LogbackLoggingSystemProperties(Environment environment) { super(environment); @@ -100,32 +126,24 @@ public class LogbackLoggingSystemProperties extends LoggingSystemProperties { } private void applyRollingPolicyProperties(PropertyResolver resolver) { - applyRollingPolicy(resolver, ROLLINGPOLICY_FILE_NAME_PATTERN, "logging.logback.rollingpolicy.file-name-pattern", - "logging.pattern.rolling-file-name"); - applyRollingPolicy(resolver, ROLLINGPOLICY_CLEAN_HISTORY_ON_START, - "logging.logback.rollingpolicy.clean-history-on-start", "logging.file.clean-history-on-start"); - applyRollingPolicy(resolver, ROLLINGPOLICY_MAX_FILE_SIZE, "logging.logback.rollingpolicy.max-file-size", - "logging.file.max-size", DataSize.class); - applyRollingPolicy(resolver, ROLLINGPOLICY_TOTAL_SIZE_CAP, "logging.logback.rollingpolicy.total-size-cap", - "logging.file.total-size-cap", DataSize.class); - applyRollingPolicy(resolver, ROLLINGPOLICY_MAX_HISTORY, "logging.logback.rollingpolicy.max-history", - "logging.file.max-history"); + applyRollingPolicy(RollingPolicySystemProperty.FILE_NAME_PATTERN, resolver); + applyRollingPolicy(RollingPolicySystemProperty.CLEAN_HISTORY_ON_START, resolver); + applyRollingPolicy(RollingPolicySystemProperty.MAX_FILE_SIZE, resolver, DataSize.class); + applyRollingPolicy(RollingPolicySystemProperty.TOTAL_SIZE_CAP, resolver, DataSize.class); + applyRollingPolicy(RollingPolicySystemProperty.MAX_HISTORY, resolver); } - private void applyRollingPolicy(PropertyResolver resolver, String systemPropertyName, String propertyName, - String deprecatedPropertyName) { - applyRollingPolicy(resolver, systemPropertyName, propertyName, deprecatedPropertyName, String.class); + private void applyRollingPolicy(RollingPolicySystemProperty property, PropertyResolver resolver) { + applyRollingPolicy(property, resolver, String.class); } - private void applyRollingPolicy(PropertyResolver resolver, String systemPropertyName, String propertyName, - String deprecatedPropertyName, Class type) { - T value = getProperty(resolver, propertyName, type); - if (value == null) { - value = getProperty(resolver, deprecatedPropertyName, type); - } + private void applyRollingPolicy(RollingPolicySystemProperty property, PropertyResolver resolver, + Class type) { + T value = getProperty(resolver, property.getApplicationPropertyName(), type); + value = (value != null) ? value : getProperty(resolver, property.getDeprecatedApplicationPropertyName(), type); if (value != null) { String stringValue = String.valueOf((value instanceof DataSize dataSize) ? dataSize.toBytes() : value); - setSystemProperty(systemPropertyName, stringValue); + setSystemProperty(property.getEnvironmentVariableName(), stringValue); } } diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/RollingPolicySystemProperty.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/RollingPolicySystemProperty.java new file mode 100644 index 0000000000..f75db8f238 --- /dev/null +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/RollingPolicySystemProperty.java @@ -0,0 +1,82 @@ +/* + * Copyright 2012-2023 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. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.logging.logback; + +/** + * Logback rolling policy system properties that can later be used by log configuration + * files. + * + * @author Phillip Webb + * @since 3.2.0 + * @see LogbackLoggingSystemProperties + */ +public enum RollingPolicySystemProperty { + + /** + * Logging system property for the rolled-over log file name pattern. + */ + FILE_NAME_PATTERN("file-name-pattern", "logging.pattern.rolling-file-name"), + + /** + * Logging system property for the clean history on start flag. + */ + CLEAN_HISTORY_ON_START("clean-history-on-start", "logging.file.clean-history-on-start"), + + /** + * Logging system property for the file log max size. + */ + MAX_FILE_SIZE("max-file-size", "logging.file.max-size"), + + /** + * Logging system property for the file total size cap. + */ + TOTAL_SIZE_CAP("total-size-cap", "logging.file.total-size-cap"), + + /** + * Logging system property for the file log max history. + */ + MAX_HISTORY("max-history", "logging.file.max-history"); + + private final String environmentVariableName; + + private final String applicationPropertyName; + + private final String deprecatedApplicationPropertyName; + + RollingPolicySystemProperty(String applicationPropertyName, String deprecatedApplicationPropertyName) { + this.environmentVariableName = "LOGBACK_ROLLINGPOLICY_" + name(); + this.applicationPropertyName = "logging.logback.rollingpolicy." + applicationPropertyName; + this.deprecatedApplicationPropertyName = deprecatedApplicationPropertyName; + } + + /** + * Return the name of environment variable that can be used to access this property. + * @return the environment variable name + */ + public String getEnvironmentVariableName() { + return this.environmentVariableName; + } + + String getApplicationPropertyName() { + return this.applicationPropertyName; + } + + String getDeprecatedApplicationPropertyName() { + return this.deprecatedApplicationPropertyName; + } + +} diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/logging/LoggingApplicationListenerIntegrationTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/logging/LoggingApplicationListenerIntegrationTests.java index 398bc82982..d7c8b12a49 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/logging/LoggingApplicationListenerIntegrationTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/logging/LoggingApplicationListenerIntegrationTests.java @@ -30,7 +30,7 @@ import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.context.event.ApplicationStartingEvent; import org.springframework.boot.logging.LogFile; import org.springframework.boot.logging.LoggingSystem; -import org.springframework.boot.logging.LoggingSystemProperties; +import org.springframework.boot.logging.LoggingSystemProperty; import org.springframework.boot.testsupport.system.CapturedOutput; import org.springframework.boot.testsupport.system.OutputCaptureExtension; import org.springframework.context.ApplicationListener; @@ -69,7 +69,7 @@ class LoggingApplicationListenerIntegrationTests { assertThat(service.logFile).hasToString(logFile); } finally { - System.clearProperty(LoggingSystemProperties.LOG_FILE); + System.clearProperty(LoggingSystemProperty.LOG_FILE.getEnvironmentVariableName()); } } 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 d083114281..4bfeac3630 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 @@ -58,7 +58,7 @@ import org.springframework.boot.logging.LoggerConfiguration; import org.springframework.boot.logging.LoggerGroups; import org.springframework.boot.logging.LoggingInitializationContext; import org.springframework.boot.logging.LoggingSystem; -import org.springframework.boot.logging.LoggingSystemProperties; +import org.springframework.boot.logging.LoggingSystemProperty; import org.springframework.boot.logging.java.JavaLoggingSystem; import org.springframework.boot.system.ApplicationPid; import org.springframework.boot.testsupport.classpath.ClassPathExclusions; @@ -470,13 +470,13 @@ class LoggingApplicationListenerTests { "logging.pattern.file=file", "logging.pattern.level=level", "logging.pattern.rolling-file-name=my.log.%d{yyyyMMdd}.%i.gz"); this.listener.initialize(this.context.getEnvironment(), this.context.getClassLoader()); - assertThat(System.getProperty(LoggingSystemProperties.CONSOLE_LOG_PATTERN)).isEqualTo("console"); - assertThat(System.getProperty(LoggingSystemProperties.FILE_LOG_PATTERN)).isEqualTo("file"); - assertThat(System.getProperty(LoggingSystemProperties.EXCEPTION_CONVERSION_WORD)).isEqualTo("conversion"); - assertThat(System.getProperty(LoggingSystemProperties.LOG_FILE)).isEqualTo(this.logFile.getAbsolutePath()); - assertThat(System.getProperty(LoggingSystemProperties.LOG_LEVEL_PATTERN)).isEqualTo("level"); - assertThat(System.getProperty(LoggingSystemProperties.LOG_PATH)).isEqualTo("path"); - assertThat(System.getProperty(LoggingSystemProperties.PID_KEY)).isNotNull(); + assertThat(getSystemProperty(LoggingSystemProperty.CONSOLE_PATTERN)).isEqualTo("console"); + assertThat(getSystemProperty(LoggingSystemProperty.FILE_PATTERN)).isEqualTo("file"); + assertThat(getSystemProperty(LoggingSystemProperty.EXCEPTION_CONVERSION_WORD)).isEqualTo("conversion"); + assertThat(getSystemProperty(LoggingSystemProperty.LOG_FILE)).isEqualTo(this.logFile.getAbsolutePath()); + assertThat(getSystemProperty(LoggingSystemProperty.LEVEL_PATTERN)).isEqualTo("level"); + assertThat(getSystemProperty(LoggingSystemProperty.LOG_PATH)).isEqualTo("path"); + assertThat(getSystemProperty(LoggingSystemProperty.PID)).isNotNull(); } @Test @@ -484,15 +484,14 @@ class LoggingApplicationListenerTests { // gh-7719 addPropertiesToEnvironment(this.context, "logging.pattern.console=console ${doesnotexist}"); this.listener.initialize(this.context.getEnvironment(), this.context.getClassLoader()); - assertThat(System.getProperty(LoggingSystemProperties.CONSOLE_LOG_PATTERN)) - .isEqualTo("console ${doesnotexist}"); + assertThat(getSystemProperty(LoggingSystemProperty.CONSOLE_PATTERN)).isEqualTo("console ${doesnotexist}"); } @Test void environmentPropertiesResolvePlaceholders() { addPropertiesToEnvironment(this.context, "logging.pattern.console=console ${pid}"); this.listener.initialize(this.context.getEnvironment(), this.context.getClassLoader()); - assertThat(System.getProperty(LoggingSystemProperties.CONSOLE_LOG_PATTERN)) + assertThat(getSystemProperty(LoggingSystemProperty.CONSOLE_PATTERN)) .isEqualTo(this.context.getEnvironment().getProperty("logging.pattern.console")); } @@ -500,7 +499,7 @@ class LoggingApplicationListenerTests { void logFilePropertiesCanReferenceSystemProperties() { addPropertiesToEnvironment(this.context, "logging.file.name=" + this.tempDir + "${PID}.log"); this.listener.initialize(this.context.getEnvironment(), this.context.getClassLoader()); - assertThat(System.getProperty(LoggingSystemProperties.LOG_FILE)) + assertThat(getSystemProperty(LoggingSystemProperty.LOG_FILE)) .isEqualTo(this.tempDir + new ApplicationPid().toString() + ".log"); } @@ -575,6 +574,10 @@ class LoggingApplicationListenerTests { assertTraceEnabled("com.foo.baz", true); } + private String getSystemProperty(LoggingSystemProperty property) { + return System.getProperty(property.getEnvironmentVariableName()); + } + private void assertTraceEnabled(String name, boolean expected) { assertThat(this.loggerContext.getLogger(name).isTraceEnabled()).isEqualTo(expected); } diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/AbstractLoggingSystemTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/AbstractLoggingSystemTests.java index 3ccc162558..c80bf5654b 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/AbstractLoggingSystemTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/AbstractLoggingSystemTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2020 the original author or authors. + * Copyright 2012-2023 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. @@ -50,8 +50,9 @@ public abstract class AbstractLoggingSystemTests { @AfterEach void clear() { - System.clearProperty(LoggingSystemProperties.LOG_FILE); - System.clearProperty(LoggingSystemProperties.PID_KEY); + for (LoggingSystemProperty property : LoggingSystemProperty.values()) { + System.getProperties().remove(property.getEnvironmentVariableName()); + } } protected final String[] getSpringConfigLocations(AbstractLoggingSystem system) { diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/LogFileTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/LogFileTests.java index 46ab410e0e..6639436655 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/LogFileTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/LogFileTests.java @@ -57,8 +57,9 @@ class LogFileTests { Properties properties = new Properties(); logFile.applyTo(properties); assertThat(logFile).hasToString("log.file"); - assertThat(properties.getProperty(LoggingSystemProperties.LOG_FILE)).isEqualTo("log.file"); - assertThat(properties.getProperty(LoggingSystemProperties.LOG_PATH)).isNull(); + assertThat(properties.getProperty(LoggingSystemProperty.LOG_FILE.getEnvironmentVariableName())) + .isEqualTo("log.file"); + assertThat(properties.getProperty(LoggingSystemProperty.LOG_PATH.getEnvironmentVariableName())).isNull(); } @Test @@ -72,9 +73,10 @@ class LogFileTests { Properties properties = new Properties(); logFile.applyTo(properties); assertThat(logFile).hasToString("logpath" + File.separatorChar + "spring.log"); - assertThat(properties.getProperty(LoggingSystemProperties.LOG_FILE)) + assertThat(properties.getProperty(LoggingSystemProperty.LOG_FILE.getEnvironmentVariableName())) .isEqualTo("logpath" + File.separatorChar + "spring.log"); - assertThat(properties.getProperty(LoggingSystemProperties.LOG_PATH)).isEqualTo("logpath"); + assertThat(properties.getProperty(LoggingSystemProperty.LOG_PATH.getEnvironmentVariableName())) + .isEqualTo("logpath"); } @Test @@ -91,8 +93,10 @@ class LogFileTests { Properties properties = new Properties(); logFile.applyTo(properties); assertThat(logFile).hasToString("log.file"); - assertThat(properties.getProperty(LoggingSystemProperties.LOG_FILE)).isEqualTo("log.file"); - assertThat(properties.getProperty(LoggingSystemProperties.LOG_PATH)).isEqualTo("logpath"); + assertThat(properties.getProperty(LoggingSystemProperty.LOG_FILE.getEnvironmentVariableName())) + .isEqualTo("log.file"); + assertThat(properties.getProperty(LoggingSystemProperty.LOG_PATH.getEnvironmentVariableName())) + .isEqualTo("logpath"); } private PropertyResolver getPropertyResolver(Map properties) { diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/LoggingSystemPropertiesTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/LoggingSystemPropertiesTests.java index 3fb0106d71..c374f0d31c 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/LoggingSystemPropertiesTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/LoggingSystemPropertiesTests.java @@ -43,8 +43,9 @@ class LoggingSystemPropertiesTests { @BeforeEach void captureSystemPropertyNames() { - System.getProperties().remove(LoggingSystemProperties.CONSOLE_LOG_CHARSET); - System.getProperties().remove(LoggingSystemProperties.FILE_LOG_CHARSET); + for (LoggingSystemProperty property : LoggingSystemProperty.values()) { + System.getProperties().remove(property.getEnvironmentVariableName()); + } this.systemPropertyNames = new HashSet<>(System.getProperties().keySet()); } @@ -56,58 +57,62 @@ class LoggingSystemPropertiesTests { @Test void pidIsSet() { new LoggingSystemProperties(new MockEnvironment()).apply(null); - assertThat(System.getProperty(LoggingSystemProperties.PID_KEY)).isNotNull(); + assertThat(getSystemProperty(LoggingSystemProperty.PID)).isNotNull(); } @Test void consoleLogPatternIsSet() { new LoggingSystemProperties(new MockEnvironment().withProperty("logging.pattern.console", "console pattern")) .apply(null); - assertThat(System.getProperty(LoggingSystemProperties.CONSOLE_LOG_PATTERN)).isEqualTo("console pattern"); + assertThat(getSystemProperty(LoggingSystemProperty.CONSOLE_PATTERN)).isEqualTo("console pattern"); } @Test void consoleCharsetWhenNoPropertyUsesUtf8() { new LoggingSystemProperties(new MockEnvironment()).apply(null); - assertThat(System.getProperty(LoggingSystemProperties.CONSOLE_LOG_CHARSET)).isEqualTo("UTF-8"); + assertThat(getSystemProperty(LoggingSystemProperty.CONSOLE_CHARSET)).isEqualTo("UTF-8"); } @Test void consoleCharsetIsSet() { new LoggingSystemProperties(new MockEnvironment().withProperty("logging.charset.console", "UTF-16")) .apply(null); - assertThat(System.getProperty(LoggingSystemProperties.CONSOLE_LOG_CHARSET)).isEqualTo("UTF-16"); + assertThat(getSystemProperty(LoggingSystemProperty.CONSOLE_CHARSET)).isEqualTo("UTF-16"); } @Test void fileLogPatternIsSet() { new LoggingSystemProperties(new MockEnvironment().withProperty("logging.pattern.file", "file pattern")) .apply(null); - assertThat(System.getProperty(LoggingSystemProperties.FILE_LOG_PATTERN)).isEqualTo("file pattern"); + assertThat(getSystemProperty(LoggingSystemProperty.FILE_PATTERN)).isEqualTo("file pattern"); } @Test void fileCharsetWhenNoPropertyUsesUtf8() { new LoggingSystemProperties(new MockEnvironment()).apply(null); - assertThat(System.getProperty(LoggingSystemProperties.FILE_LOG_CHARSET)).isEqualTo("UTF-8"); + assertThat(getSystemProperty(LoggingSystemProperty.FILE_CHARSET)).isEqualTo("UTF-8"); } @Test void fileCharsetIsSet() { new LoggingSystemProperties(new MockEnvironment().withProperty("logging.charset.file", "UTF-16")).apply(null); - assertThat(System.getProperty(LoggingSystemProperties.FILE_LOG_CHARSET)).isEqualTo("UTF-16"); + assertThat(getSystemProperty(LoggingSystemProperty.FILE_CHARSET)).isEqualTo("UTF-16"); } @Test void consoleLogPatternCanReferencePid() { new LoggingSystemProperties(environment("logging.pattern.console", "${PID:unknown}")).apply(null); - assertThat(System.getProperty(LoggingSystemProperties.CONSOLE_LOG_PATTERN)).matches("[0-9]+"); + assertThat(getSystemProperty(LoggingSystemProperty.CONSOLE_PATTERN)).matches("[0-9]+"); } @Test void fileLogPatternCanReferencePid() { new LoggingSystemProperties(environment("logging.pattern.file", "${PID:unknown}")).apply(null); - assertThat(System.getProperty(LoggingSystemProperties.FILE_LOG_PATTERN)).matches("[0-9]+"); + assertThat(getSystemProperty(LoggingSystemProperty.FILE_PATTERN)).matches("[0-9]+"); + } + + private String getSystemProperty(LoggingSystemProperty property) { + return System.getProperty(property.getEnvironmentVariableName()); } private Environment environment(String key, Object value) { diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/java/JavaLoggingSystemTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/java/JavaLoggingSystemTests.java index cb73992349..c13156c843 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/java/JavaLoggingSystemTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/java/JavaLoggingSystemTests.java @@ -33,7 +33,7 @@ import org.springframework.boot.logging.AbstractLoggingSystemTests; import org.springframework.boot.logging.LogLevel; import org.springframework.boot.logging.LoggerConfiguration; import org.springframework.boot.logging.LoggingSystem; -import org.springframework.boot.logging.LoggingSystemProperties; +import org.springframework.boot.logging.LoggingSystemProperty; import org.springframework.boot.testsupport.system.CapturedOutput; import org.springframework.boot.testsupport.system.OutputCaptureExtension; import org.springframework.util.ClassUtils; @@ -113,7 +113,7 @@ class JavaLoggingSystemTests extends AbstractLoggingSystemTests { @Test void testSystemPropertyInitializesFormat(CapturedOutput output) { - System.setProperty(LoggingSystemProperties.PID_KEY, "1234"); + System.setProperty(LoggingSystemProperty.PID.getEnvironmentVariableName(), "1234"); this.loggingSystem.beforeInitialize(); this.loggingSystem.initialize(null, "classpath:" + ClassUtils.addResourcePathToPackagePath(getClass(), "logging.properties"), null); diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystemTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystemTests.java index 7aabe82966..a177de6bae 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystemTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystemTests.java @@ -53,7 +53,7 @@ import org.springframework.boot.logging.LogLevel; import org.springframework.boot.logging.LoggerConfiguration; import org.springframework.boot.logging.LoggingInitializationContext; import org.springframework.boot.logging.LoggingSystem; -import org.springframework.boot.logging.LoggingSystemProperties; +import org.springframework.boot.logging.LoggingSystemProperty; import org.springframework.boot.testsupport.classpath.ClassPathExclusions; import org.springframework.boot.testsupport.logging.ConfigureClasspathToPreferLog4j2; import org.springframework.boot.testsupport.system.CapturedOutput; @@ -361,7 +361,7 @@ class Log4J2LoggingSystemTests extends AbstractLoggingSystemTests { @Test void customExceptionConversionWord(CapturedOutput output) { - System.setProperty(LoggingSystemProperties.EXCEPTION_CONVERSION_WORD, "%ex"); + System.setProperty(LoggingSystemProperty.EXCEPTION_CONVERSION_WORD.getEnvironmentVariableName(), "%ex"); try { this.loggingSystem.beforeInitialize(); this.logger.info("Hidden"); @@ -373,7 +373,7 @@ class Log4J2LoggingSystemTests extends AbstractLoggingSystemTests { assertThat(output).contains("java.lang.RuntimeException: Expected").doesNotContain("Wrapped by:"); } finally { - System.clearProperty(LoggingSystemProperties.EXCEPTION_CONVERSION_WORD); + System.clearProperty(LoggingSystemProperty.EXCEPTION_CONVERSION_WORD.getEnvironmentVariableName()); } } diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/log4j2/Log4j2FileXmlTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/log4j2/Log4j2FileXmlTests.java index e5ea6eb56b..53517e76dc 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/log4j2/Log4j2FileXmlTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/log4j2/Log4j2FileXmlTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2022 the original author or authors. + * Copyright 2012-2023 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. @@ -23,7 +23,7 @@ import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; -import org.springframework.boot.logging.LoggingSystemProperties; +import org.springframework.boot.logging.LoggingSystemProperty; import static org.assertj.core.api.Assertions.assertThat; @@ -42,7 +42,9 @@ class Log4j2FileXmlTests extends Log4j2XmlTests { @AfterEach void stopConfiguration() { super.stopConfiguration(); - System.clearProperty(LoggingSystemProperties.LOG_FILE); + for (LoggingSystemProperty property : LoggingSystemProperty.values()) { + System.getProperties().remove(property.getEnvironmentVariableName()); + } } @Test @@ -52,7 +54,7 @@ class Log4j2FileXmlTests extends Log4j2XmlTests { @Test void whenLogExceptionConversionWordIsSetThenFileAppenderUsesIt() { - withSystemProperty(LoggingSystemProperties.EXCEPTION_CONVERSION_WORD, "custom", + withSystemProperty(LoggingSystemProperty.EXCEPTION_CONVERSION_WORD.getEnvironmentVariableName(), "custom", () -> assertThat(fileAppenderPattern()).contains("custom")); } @@ -63,7 +65,7 @@ class Log4j2FileXmlTests extends Log4j2XmlTests { @Test void whenLogLevelPatternIsSetThenFileAppenderUsesIt() { - withSystemProperty(LoggingSystemProperties.LOG_LEVEL_PATTERN, "custom", + withSystemProperty(LoggingSystemProperty.LEVEL_PATTERN.getEnvironmentVariableName(), "custom", () -> assertThat(fileAppenderPattern()).contains("custom")); } @@ -74,7 +76,7 @@ class Log4j2FileXmlTests extends Log4j2XmlTests { @Test void whenLogDateformatPatternIsSetThenFileAppenderUsesIt() { - withSystemProperty(LoggingSystemProperties.LOG_DATEFORMAT_PATTERN, "dd-MM-yyyy", + withSystemProperty(LoggingSystemProperty.DATEFORMAT_PATTERN.getEnvironmentVariableName(), "dd-MM-yyyy", () -> assertThat(fileAppenderPattern()).contains("dd-MM-yyyy")); } @@ -85,7 +87,8 @@ class Log4j2FileXmlTests extends Log4j2XmlTests { @Override protected void prepareConfiguration() { - System.setProperty(LoggingSystemProperties.LOG_FILE, new File(this.temp, "test.log").getAbsolutePath()); + System.setProperty(LoggingSystemProperty.LOG_FILE.getEnvironmentVariableName(), + new File(this.temp, "test.log").getAbsolutePath()); super.prepareConfiguration(); } diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/log4j2/Log4j2XmlTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/log4j2/Log4j2XmlTests.java index 850d7a4d6b..86ecd1e293 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/log4j2/Log4j2XmlTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/log4j2/Log4j2XmlTests.java @@ -27,7 +27,7 @@ import org.apache.logging.log4j.core.layout.PatternLayout; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Test; -import org.springframework.boot.logging.LoggingSystemProperties; +import org.springframework.boot.logging.LoggingSystemProperty; import static org.assertj.core.api.Assertions.assertThat; @@ -53,7 +53,7 @@ class Log4j2XmlTests { @Test void whenLogExceptionConversionWordIsSetThenConsoleUsesIt() { - withSystemProperty(LoggingSystemProperties.EXCEPTION_CONVERSION_WORD, "custom", + withSystemProperty(LoggingSystemProperty.EXCEPTION_CONVERSION_WORD.getEnvironmentVariableName(), "custom", () -> assertThat(consolePattern()).contains("custom")); } @@ -64,7 +64,7 @@ class Log4j2XmlTests { @Test void whenLogLevelPatternIsSetThenConsoleUsesIt() { - withSystemProperty(LoggingSystemProperties.LOG_LEVEL_PATTERN, "custom", + withSystemProperty(LoggingSystemProperty.LEVEL_PATTERN.getEnvironmentVariableName(), "custom", () -> assertThat(consolePattern()).contains("custom")); } @@ -75,7 +75,7 @@ class Log4j2XmlTests { @Test void whenLogDateformatPatternIsSetThenConsoleUsesIt() { - withSystemProperty(LoggingSystemProperties.LOG_DATEFORMAT_PATTERN, "dd-MM-yyyy", + withSystemProperty(LoggingSystemProperty.DATEFORMAT_PATTERN.getEnvironmentVariableName(), "dd-MM-yyyy", () -> assertThat(consolePattern()).contains("dd-MM-yyyy")); } diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/logback/LogbackLoggingSystemPropertiesTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/logback/LogbackLoggingSystemPropertiesTests.java index 2ab61b672f..45c82c533d 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/logback/LogbackLoggingSystemPropertiesTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/logback/LogbackLoggingSystemPropertiesTests.java @@ -26,6 +26,7 @@ import org.junit.jupiter.api.Test; import org.springframework.boot.convert.ApplicationConversionService; import org.springframework.boot.logging.LoggingSystemProperties; +import org.springframework.boot.logging.LoggingSystemProperty; import org.springframework.core.convert.support.ConfigurableConversionService; import org.springframework.mock.env.MockEnvironment; @@ -44,8 +45,9 @@ class LogbackLoggingSystemPropertiesTests { @BeforeEach void captureSystemPropertyNames() { - System.getProperties().remove(LoggingSystemProperties.CONSOLE_LOG_CHARSET); - System.getProperties().remove(LoggingSystemProperties.FILE_LOG_CHARSET); + for (LoggingSystemProperty property : LoggingSystemProperty.values()) { + System.getProperties().remove(property.getEnvironmentVariableName()); + } this.systemPropertyNames = new HashSet<>(System.getProperties().keySet()); this.environment = new MockEnvironment(); this.environment @@ -62,7 +64,8 @@ class LogbackLoggingSystemPropertiesTests { void applySetsStandardSystemProperties() { this.environment.setProperty("logging.pattern.console", "boot"); new LogbackLoggingSystemProperties(this.environment).apply(); - assertThat(System.getProperties()).containsEntry(LoggingSystemProperties.CONSOLE_LOG_PATTERN, "boot"); + assertThat(System.getProperties()) + .containsEntry(LoggingSystemProperty.CONSOLE_PATTERN.getEnvironmentVariableName(), "boot"); } @Test @@ -74,11 +77,11 @@ class LogbackLoggingSystemPropertiesTests { this.environment.setProperty("logging.logback.rollingpolicy.max-history", "mh"); new LogbackLoggingSystemProperties(this.environment).apply(); assertThat(System.getProperties()) - .containsEntry(LogbackLoggingSystemProperties.ROLLINGPOLICY_FILE_NAME_PATTERN, "fnp") - .containsEntry(LogbackLoggingSystemProperties.ROLLINGPOLICY_CLEAN_HISTORY_ON_START, "chos") - .containsEntry(LogbackLoggingSystemProperties.ROLLINGPOLICY_MAX_FILE_SIZE, "1024") - .containsEntry(LogbackLoggingSystemProperties.ROLLINGPOLICY_TOTAL_SIZE_CAP, "2048") - .containsEntry(LogbackLoggingSystemProperties.ROLLINGPOLICY_MAX_HISTORY, "mh"); + .containsEntry(RollingPolicySystemProperty.FILE_NAME_PATTERN.getEnvironmentVariableName(), "fnp") + .containsEntry(RollingPolicySystemProperty.CLEAN_HISTORY_ON_START.getEnvironmentVariableName(), "chos") + .containsEntry(RollingPolicySystemProperty.MAX_FILE_SIZE.getEnvironmentVariableName(), "1024") + .containsEntry(RollingPolicySystemProperty.TOTAL_SIZE_CAP.getEnvironmentVariableName(), "2048") + .containsEntry(RollingPolicySystemProperty.MAX_HISTORY.getEnvironmentVariableName(), "mh"); } @Test @@ -90,24 +93,24 @@ class LogbackLoggingSystemPropertiesTests { this.environment.setProperty("logging.file.max-history", "mh"); new LogbackLoggingSystemProperties(this.environment).apply(); assertThat(System.getProperties()) - .containsEntry(LogbackLoggingSystemProperties.ROLLINGPOLICY_FILE_NAME_PATTERN, "fnp") - .containsEntry(LogbackLoggingSystemProperties.ROLLINGPOLICY_CLEAN_HISTORY_ON_START, "chos") - .containsEntry(LogbackLoggingSystemProperties.ROLLINGPOLICY_MAX_FILE_SIZE, "1024") - .containsEntry(LogbackLoggingSystemProperties.ROLLINGPOLICY_TOTAL_SIZE_CAP, "2048") - .containsEntry(LogbackLoggingSystemProperties.ROLLINGPOLICY_MAX_HISTORY, "mh"); + .containsEntry(RollingPolicySystemProperty.FILE_NAME_PATTERN.getEnvironmentVariableName(), "fnp") + .containsEntry(RollingPolicySystemProperty.CLEAN_HISTORY_ON_START.getEnvironmentVariableName(), "chos") + .containsEntry(RollingPolicySystemProperty.MAX_FILE_SIZE.getEnvironmentVariableName(), "1024") + .containsEntry(RollingPolicySystemProperty.TOTAL_SIZE_CAP.getEnvironmentVariableName(), "2048") + .containsEntry(RollingPolicySystemProperty.MAX_HISTORY.getEnvironmentVariableName(), "mh"); } @Test void consoleCharsetWhenNoPropertyUsesDefault() { new LoggingSystemProperties(new MockEnvironment()).apply(null); - assertThat(System.getProperty(LoggingSystemProperties.CONSOLE_LOG_CHARSET)) + assertThat(System.getProperty(LoggingSystemProperty.CONSOLE_CHARSET.getEnvironmentVariableName())) .isEqualTo(Charset.defaultCharset().name()); } @Test void fileCharsetWhenNoPropertyUsesDefault() { new LoggingSystemProperties(new MockEnvironment()).apply(null); - assertThat(System.getProperty(LoggingSystemProperties.FILE_LOG_CHARSET)) + assertThat(System.getProperty(LoggingSystemProperty.FILE_CHARSET.getEnvironmentVariableName())) .isEqualTo(Charset.defaultCharset().name()); } diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/logback/LogbackLoggingSystemTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/logback/LogbackLoggingSystemTests.java index a0a27b077e..515d09a1e1 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/logback/LogbackLoggingSystemTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/logback/LogbackLoggingSystemTests.java @@ -56,6 +56,7 @@ import org.springframework.boot.logging.LoggerConfiguration; import org.springframework.boot.logging.LoggingInitializationContext; import org.springframework.boot.logging.LoggingSystem; import org.springframework.boot.logging.LoggingSystemProperties; +import org.springframework.boot.logging.LoggingSystemProperty; import org.springframework.boot.testsupport.classpath.ClassPathOverrides; import org.springframework.boot.testsupport.system.CapturedOutput; import org.springframework.boot.testsupport.system.OutputCaptureExtension; @@ -102,8 +103,9 @@ class LogbackLoggingSystemTests extends AbstractLoggingSystemTests { @BeforeEach void setup() { - System.getProperties().remove(LoggingSystemProperties.CONSOLE_LOG_CHARSET); - System.getProperties().remove(LoggingSystemProperties.FILE_LOG_CHARSET); + for (LoggingSystemProperty property : LoggingSystemProperty.values()) { + System.getProperties().remove(property.getEnvironmentVariableName()); + } this.systemPropertyNames = new HashSet<>(System.getProperties().keySet()); this.loggingSystem.cleanUp(); this.logger = ((LoggerContext) LoggerFactory.getILoggerFactory()).getLogger(getClass()); @@ -503,7 +505,7 @@ class LogbackLoggingSystemTests extends AbstractLoggingSystemTests { @Test void customExceptionConversionWord(CapturedOutput output) { - System.setProperty(LoggingSystemProperties.EXCEPTION_CONVERSION_WORD, "%ex"); + System.setProperty(LoggingSystemProperty.EXCEPTION_CONVERSION_WORD.getEnvironmentVariableName(), "%ex"); try { this.loggingSystem.beforeInitialize(); this.logger.info("Hidden"); @@ -514,7 +516,7 @@ class LogbackLoggingSystemTests extends AbstractLoggingSystemTests { assertThat(output).contains("java.lang.RuntimeException: Expected").doesNotContain("Wrapped by:"); } finally { - System.clearProperty(LoggingSystemProperties.EXCEPTION_CONVERSION_WORD); + System.clearProperty(LoggingSystemProperty.EXCEPTION_CONVERSION_WORD.getEnvironmentVariableName()); } } @@ -525,7 +527,8 @@ class LogbackLoggingSystemTests extends AbstractLoggingSystemTests { this.logger.info("Hidden"); LogFile logFile = getLogFile(tmpDir() + "/example.log", null, false); initialize(this.initializationContext, "classpath:logback-nondefault.xml", logFile); - assertThat(System.getProperty(LoggingSystemProperties.LOG_FILE)).endsWith("example.log"); + assertThat(System.getProperty(LoggingSystemProperty.LOG_FILE.getEnvironmentVariableName())) + .endsWith("example.log"); } @Test