From 4c5de416ef3009b8f81247c66e479c3a82be3e89 Mon Sep 17 00:00:00 2001 From: Johnny Lim Date: Fri, 1 Dec 2017 23:00:48 +0900 Subject: [PATCH] Use LoggingSystemProperties constants See gh-11240 --- .../boot/StartupInfoLogger.java | 3 +- .../boot/logging/java/SimpleFormatter.java | 4 ++- .../logging/logback/LogbackLoggingSystem.java | 5 +-- .../LoggingApplicationListenerTests.java | 33 ++++++++++--------- .../logging/AbstractLoggingSystemTests.java | 4 +-- .../boot/logging/LogFileTests.java | 12 +++---- .../logging/java/JavaLoggingSystemTests.java | 3 +- .../log4j2/Log4J2LoggingSystemTests.java | 5 +-- .../logback/LogbackLoggingSystemTests.java | 7 ++-- 9 files changed, 42 insertions(+), 34 deletions(-) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/StartupInfoLogger.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/StartupInfoLogger.java index 9ebf9580c84..a36b72e18c7 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/StartupInfoLogger.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/StartupInfoLogger.java @@ -22,6 +22,7 @@ import java.util.concurrent.Callable; import org.apache.commons.logging.Log; +import org.springframework.boot.logging.LoggingSystemProperties; import org.springframework.context.ApplicationContext; import org.springframework.util.Assert; import org.springframework.util.ClassUtils; @@ -108,7 +109,7 @@ class StartupInfoLogger { } private String getPid() { - return getValue(" with PID ", () -> System.getProperty("PID")); + return getValue(" with PID ", () -> System.getProperty(LoggingSystemProperties.PID_KEY)); } private String getContext() { 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 374df042a01..3d7ef319e65 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 @@ -22,6 +22,8 @@ import java.util.Date; import java.util.logging.Formatter; import java.util.logging.LogRecord; +import org.springframework.boot.logging.LoggingSystemProperties; + /** * Simple 'Java Logging' {@link Formatter}. * @@ -33,7 +35,7 @@ public class SimpleFormatter extends Formatter { private final String format = getOrUseDefault("LOG_FORMAT", DEFAULT_FORMAT); - private final String pid = getOrUseDefault("PID", "????"); + private final String pid = getOrUseDefault(LoggingSystemProperties.PID_KEY, "????"); private final Date date = new Date(); diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/LogbackLoggingSystem.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/LogbackLoggingSystem.java index 2ffe8d7a463..e83274e57fc 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/LogbackLoggingSystem.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/LogbackLoggingSystem.java @@ -42,6 +42,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.Slf4JLoggingSystem; import org.springframework.core.env.Environment; import org.springframework.util.Assert; @@ -127,9 +128,9 @@ public class LogbackLoggingSystem extends Slf4JLoggingSystem { stopAndReset(context); LogbackConfigurator configurator = new LogbackConfigurator(context); Environment environment = initializationContext.getEnvironment(); - context.putProperty("LOG_LEVEL_PATTERN", environment.resolvePlaceholders( + context.putProperty(LoggingSystemProperties.LOG_LEVEL_PATTERN, environment.resolvePlaceholders( "${logging.pattern.level:${LOG_LEVEL_PATTERN:%5p}}")); - context.putProperty("LOG_DATEFORMAT_PATTERN", environment.resolvePlaceholders( + context.putProperty(LoggingSystemProperties.LOG_DATEFORMAT_PATTERN, environment.resolvePlaceholders( "${logging.pattern.dateformat:${LOG_DATEFORMAT_PATTERN:yyyy-MM-dd HH:mm:ss.SSS}}")); new DefaultLogbackConfiguration(initializationContext, logFile) .apply(configurator); 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 ac18cad9d3d..ed7fd456a46 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 @@ -49,6 +49,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.java.JavaLoggingSystem; import org.springframework.boot.testsupport.rule.OutputCapture; import org.springframework.boot.testsupport.runner.classpath.ClassPathExclusions; @@ -117,13 +118,13 @@ public class LoggingApplicationListenerTests { loggingSystem.setLogLevel("ROOT", LogLevel.INFO); loggingSystem.cleanUp(); System.clearProperty(LoggingSystem.class.getName()); - System.clearProperty("LOG_FILE"); - System.clearProperty("LOG_PATH"); - System.clearProperty("PID"); - System.clearProperty("LOG_EXCEPTION_CONVERSION_WORD"); - System.clearProperty("CONSOLE_LOG_PATTERN"); - System.clearProperty("FILE_LOG_PATTERN"); - System.clearProperty("LOG_LEVEL_PATTERN"); + System.clearProperty(LoggingSystemProperties.LOG_FILE); + System.clearProperty(LoggingSystemProperties.LOG_PATH); + System.clearProperty(LoggingSystemProperties.PID_KEY); + System.clearProperty(LoggingSystemProperties.EXCEPTION_CONVERSION_WORD); + System.clearProperty(LoggingSystemProperties.CONSOLE_LOG_PATTERN); + System.clearProperty(LoggingSystemProperties.FILE_LOG_PATTERN); + System.clearProperty(LoggingSystemProperties.LOG_LEVEL_PATTERN); System.clearProperty(LoggingSystem.SYSTEM_PROPERTY); if (this.context != null) { this.context.close(); @@ -481,14 +482,14 @@ public class LoggingApplicationListenerTests { "logging.pattern.file=file", "logging.pattern.level=level"); this.initializer.initialize(this.context.getEnvironment(), this.context.getClassLoader()); - assertThat(System.getProperty("CONSOLE_LOG_PATTERN")).isEqualTo("console"); - assertThat(System.getProperty("FILE_LOG_PATTERN")).isEqualTo("file"); - assertThat(System.getProperty("LOG_EXCEPTION_CONVERSION_WORD")) + 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("LOG_FILE")).isEqualTo("target/log"); - assertThat(System.getProperty("LOG_LEVEL_PATTERN")).isEqualTo("level"); - assertThat(System.getProperty("LOG_PATH")).isEqualTo("path"); - assertThat(System.getProperty("PID")).isNotNull(); + assertThat(System.getProperty(LoggingSystemProperties.LOG_FILE)).isEqualTo("target/log"); + assertThat(System.getProperty(LoggingSystemProperties.LOG_LEVEL_PATTERN)).isEqualTo("level"); + assertThat(System.getProperty(LoggingSystemProperties.LOG_PATH)).isEqualTo("path"); + assertThat(System.getProperty(LoggingSystemProperties.PID_KEY)).isNotNull(); } @Test @@ -498,7 +499,7 @@ public class LoggingApplicationListenerTests { "logging.pattern.console=console ${pid}"); this.initializer.initialize(this.context.getEnvironment(), this.context.getClassLoader()); - assertThat(System.getProperty("CONSOLE_LOG_PATTERN")).isEqualTo("console ${pid}"); + assertThat(System.getProperty(LoggingSystemProperties.CONSOLE_LOG_PATTERN)).isEqualTo("console ${pid}"); } @Test @@ -507,7 +508,7 @@ public class LoggingApplicationListenerTests { "logging.file=target/${PID}.log"); this.initializer.initialize(this.context.getEnvironment(), this.context.getClassLoader()); - assertThat(System.getProperty("LOG_FILE")) + assertThat(System.getProperty(LoggingSystemProperties.LOG_FILE)) .isEqualTo("target/" + new ApplicationPid().toString() + ".log"); } 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 de881b0cc8b..009c9a92fe3 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 @@ -53,8 +53,8 @@ public abstract class AbstractLoggingSystemTests { @After public void clear() { - System.clearProperty("LOG_FILE"); - System.clearProperty("PID"); + System.clearProperty(LoggingSystemProperties.LOG_FILE); + System.clearProperty(LoggingSystemProperties.PID_KEY); } 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 8d59dc3be7b..8479f80787d 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 @@ -51,8 +51,8 @@ public class LogFileTests { Properties properties = new Properties(); logFile.applyTo(properties); assertThat(logFile.toString()).isEqualTo("log.file"); - assertThat(properties.getProperty("LOG_FILE")).isEqualTo("log.file"); - assertThat(properties.getProperty("LOG_PATH")).isNull(); + assertThat(properties.getProperty(LoggingSystemProperties.LOG_FILE)).isEqualTo("log.file"); + assertThat(properties.getProperty(LoggingSystemProperties.LOG_PATH)).isNull(); } @Test @@ -62,8 +62,8 @@ public class LogFileTests { Properties properties = new Properties(); logFile.applyTo(properties); assertThat(logFile.toString()).isEqualTo("logpath/spring.log"); - assertThat(properties.getProperty("LOG_FILE")).isEqualTo("logpath/spring.log"); - assertThat(properties.getProperty("LOG_PATH")).isEqualTo("logpath"); + assertThat(properties.getProperty(LoggingSystemProperties.LOG_FILE)).isEqualTo("logpath/spring.log"); + assertThat(properties.getProperty(LoggingSystemProperties.LOG_PATH)).isEqualTo("logpath"); } @Test @@ -73,8 +73,8 @@ public class LogFileTests { Properties properties = new Properties(); logFile.applyTo(properties); assertThat(logFile.toString()).isEqualTo("log.file"); - assertThat(properties.getProperty("LOG_FILE")).isEqualTo("log.file"); - assertThat(properties.getProperty("LOG_PATH")).isEqualTo("logpath"); + assertThat(properties.getProperty(LoggingSystemProperties.LOG_FILE)).isEqualTo("log.file"); + assertThat(properties.getProperty(LoggingSystemProperties.LOG_PATH)).isEqualTo("logpath"); } private PropertyResolver getPropertyResolver(String file, String path) { 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 4f9b6f36aba..dad4ca69adf 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 @@ -34,6 +34,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.testsupport.rule.OutputCapture; import org.springframework.util.ClassUtils; import org.springframework.util.StringUtils; @@ -117,7 +118,7 @@ public class JavaLoggingSystemTests extends AbstractLoggingSystemTests { @Test public void testSystemPropertyInitializesFormat() throws Exception { - System.setProperty("PID", "1234"); + System.setProperty(LoggingSystemProperties.PID_KEY, "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 d978de8ce73..5cca9f5ad29 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 @@ -41,6 +41,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.testsupport.assertj.Matched; import org.springframework.boot.testsupport.rule.OutputCapture; import org.springframework.util.FileCopyUtils; @@ -257,7 +258,7 @@ public class Log4J2LoggingSystemTests extends AbstractLoggingSystemTests { @Test public void customExceptionConversionWord() throws Exception { - System.setProperty("LOG_EXCEPTION_CONVERSION_WORD", "%ex"); + System.setProperty(LoggingSystemProperties.EXCEPTION_CONVERSION_WORD, "%ex"); try { this.loggingSystem.beforeInitialize(); this.logger.info("Hidden"); @@ -273,7 +274,7 @@ public class Log4J2LoggingSystemTests extends AbstractLoggingSystemTests { assertThat(fileContents).is(Matched.by(expectedOutput)); } finally { - System.clearProperty("LOG_EXCEPTION_CONVERSION_WORD"); + System.clearProperty(LoggingSystemProperties.EXCEPTION_CONVERSION_WORD); } } 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 3417359ef03..45ca6273b16 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 @@ -49,6 +49,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.testsupport.assertj.Matched; import org.springframework.boot.testsupport.rule.OutputCapture; import org.springframework.mock.env.MockEnvironment; @@ -390,7 +391,7 @@ public class LogbackLoggingSystemTests extends AbstractLoggingSystemTests { @Test public void customExceptionConversionWord() throws Exception { - System.setProperty("LOG_EXCEPTION_CONVERSION_WORD", "%ex"); + System.setProperty(LoggingSystemProperties.EXCEPTION_CONVERSION_WORD, "%ex"); try { this.loggingSystem.beforeInitialize(); this.logger.info("Hidden"); @@ -407,7 +408,7 @@ public class LogbackLoggingSystemTests extends AbstractLoggingSystemTests { assertThat(fileContents).is(Matched.by(expectedOutput)); } finally { - System.clearProperty("LOG_EXCEPTION_CONVERSION_WORD"); + System.clearProperty(LoggingSystemProperties.EXCEPTION_CONVERSION_WORD); } } @@ -419,7 +420,7 @@ public class LogbackLoggingSystemTests extends AbstractLoggingSystemTests { LogFile logFile = getLogFile(tmpDir() + "/example.log", null, false); this.loggingSystem.initialize(this.initializationContext, "classpath:logback-nondefault.xml", logFile); - assertThat(System.getProperty("LOG_FILE")).endsWith("example.log"); + assertThat(System.getProperty(LoggingSystemProperties.LOG_FILE)).endsWith("example.log"); } @Test