Merge pull request #11240 from Johnny Lim

* gh-11240:
  Polish "Use LoggingSystemProperties constants"
  Use LoggingSystemProperties constants
pull/11272/merge
Andy Wilkinson 7 years ago
commit 154d9284a8

@ -108,7 +108,7 @@ class StartupInfoLogger {
} }
private String getPid() { private String getPid() {
return getValue(" with PID ", () -> System.getProperty("PID")); return getValue(" with PID ", () -> new ApplicationPid().toString());
} }
private String getContext() { private String getContext() {

@ -22,6 +22,8 @@ import java.util.Date;
import java.util.logging.Formatter; import java.util.logging.Formatter;
import java.util.logging.LogRecord; import java.util.logging.LogRecord;
import org.springframework.boot.logging.LoggingSystemProperties;
/** /**
* Simple 'Java Logging' {@link Formatter}. * 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 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(); private final Date date = new Date();

@ -42,6 +42,7 @@ import org.springframework.boot.logging.LogLevel;
import org.springframework.boot.logging.LoggerConfiguration; import org.springframework.boot.logging.LoggerConfiguration;
import org.springframework.boot.logging.LoggingInitializationContext; import org.springframework.boot.logging.LoggingInitializationContext;
import org.springframework.boot.logging.LoggingSystem; import org.springframework.boot.logging.LoggingSystem;
import org.springframework.boot.logging.LoggingSystemProperties;
import org.springframework.boot.logging.Slf4JLoggingSystem; import org.springframework.boot.logging.Slf4JLoggingSystem;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
import org.springframework.util.Assert; import org.springframework.util.Assert;
@ -127,10 +128,12 @@ public class LogbackLoggingSystem extends Slf4JLoggingSystem {
stopAndReset(context); stopAndReset(context);
LogbackConfigurator configurator = new LogbackConfigurator(context); LogbackConfigurator configurator = new LogbackConfigurator(context);
Environment environment = initializationContext.getEnvironment(); Environment environment = initializationContext.getEnvironment();
context.putProperty("LOG_LEVEL_PATTERN", environment.resolvePlaceholders( context.putProperty(LoggingSystemProperties.LOG_LEVEL_PATTERN,
"${logging.pattern.level:${LOG_LEVEL_PATTERN:%5p}}")); environment.resolvePlaceholders(
context.putProperty("LOG_DATEFORMAT_PATTERN", environment.resolvePlaceholders( "${logging.pattern.level:${LOG_LEVEL_PATTERN:%5p}}"));
"${logging.pattern.dateformat:${LOG_DATEFORMAT_PATTERN:yyyy-MM-dd HH:mm:ss.SSS}}")); 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) new DefaultLogbackConfiguration(initializationContext, logFile)
.apply(configurator); .apply(configurator);
context.setPackagingDataEnabled(true); context.setPackagingDataEnabled(true);

@ -49,6 +49,7 @@ import org.springframework.boot.logging.LogLevel;
import org.springframework.boot.logging.LoggerConfiguration; import org.springframework.boot.logging.LoggerConfiguration;
import org.springframework.boot.logging.LoggingInitializationContext; import org.springframework.boot.logging.LoggingInitializationContext;
import org.springframework.boot.logging.LoggingSystem; import org.springframework.boot.logging.LoggingSystem;
import org.springframework.boot.logging.LoggingSystemProperties;
import org.springframework.boot.logging.java.JavaLoggingSystem; import org.springframework.boot.logging.java.JavaLoggingSystem;
import org.springframework.boot.testsupport.rule.OutputCapture; import org.springframework.boot.testsupport.rule.OutputCapture;
import org.springframework.boot.testsupport.runner.classpath.ClassPathExclusions; import org.springframework.boot.testsupport.runner.classpath.ClassPathExclusions;
@ -117,13 +118,13 @@ public class LoggingApplicationListenerTests {
loggingSystem.setLogLevel("ROOT", LogLevel.INFO); loggingSystem.setLogLevel("ROOT", LogLevel.INFO);
loggingSystem.cleanUp(); loggingSystem.cleanUp();
System.clearProperty(LoggingSystem.class.getName()); System.clearProperty(LoggingSystem.class.getName());
System.clearProperty("LOG_FILE"); System.clearProperty(LoggingSystemProperties.LOG_FILE);
System.clearProperty("LOG_PATH"); System.clearProperty(LoggingSystemProperties.LOG_PATH);
System.clearProperty("PID"); System.clearProperty(LoggingSystemProperties.PID_KEY);
System.clearProperty("LOG_EXCEPTION_CONVERSION_WORD"); System.clearProperty(LoggingSystemProperties.EXCEPTION_CONVERSION_WORD);
System.clearProperty("CONSOLE_LOG_PATTERN"); System.clearProperty(LoggingSystemProperties.CONSOLE_LOG_PATTERN);
System.clearProperty("FILE_LOG_PATTERN"); System.clearProperty(LoggingSystemProperties.FILE_LOG_PATTERN);
System.clearProperty("LOG_LEVEL_PATTERN"); System.clearProperty(LoggingSystemProperties.LOG_LEVEL_PATTERN);
System.clearProperty(LoggingSystem.SYSTEM_PROPERTY); System.clearProperty(LoggingSystem.SYSTEM_PROPERTY);
if (this.context != null) { if (this.context != null) {
this.context.close(); this.context.close();
@ -481,14 +482,19 @@ public class LoggingApplicationListenerTests {
"logging.pattern.file=file", "logging.pattern.level=level"); "logging.pattern.file=file", "logging.pattern.level=level");
this.initializer.initialize(this.context.getEnvironment(), this.initializer.initialize(this.context.getEnvironment(),
this.context.getClassLoader()); this.context.getClassLoader());
assertThat(System.getProperty("CONSOLE_LOG_PATTERN")).isEqualTo("console"); assertThat(System.getProperty(LoggingSystemProperties.CONSOLE_LOG_PATTERN))
assertThat(System.getProperty("FILE_LOG_PATTERN")).isEqualTo("file"); .isEqualTo("console");
assertThat(System.getProperty("LOG_EXCEPTION_CONVERSION_WORD")) assertThat(System.getProperty(LoggingSystemProperties.FILE_LOG_PATTERN))
.isEqualTo("file");
assertThat(System.getProperty(LoggingSystemProperties.EXCEPTION_CONVERSION_WORD))
.isEqualTo("conversion"); .isEqualTo("conversion");
assertThat(System.getProperty("LOG_FILE")).isEqualTo("target/log"); assertThat(System.getProperty(LoggingSystemProperties.LOG_FILE))
assertThat(System.getProperty("LOG_LEVEL_PATTERN")).isEqualTo("level"); .isEqualTo("target/log");
assertThat(System.getProperty("LOG_PATH")).isEqualTo("path"); assertThat(System.getProperty(LoggingSystemProperties.LOG_LEVEL_PATTERN))
assertThat(System.getProperty("PID")).isNotNull(); .isEqualTo("level");
assertThat(System.getProperty(LoggingSystemProperties.LOG_PATH))
.isEqualTo("path");
assertThat(System.getProperty(LoggingSystemProperties.PID_KEY)).isNotNull();
} }
@Test @Test
@ -498,7 +504,8 @@ public class LoggingApplicationListenerTests {
"logging.pattern.console=console ${pid}"); "logging.pattern.console=console ${pid}");
this.initializer.initialize(this.context.getEnvironment(), this.initializer.initialize(this.context.getEnvironment(),
this.context.getClassLoader()); this.context.getClassLoader());
assertThat(System.getProperty("CONSOLE_LOG_PATTERN")).isEqualTo("console ${pid}"); assertThat(System.getProperty(LoggingSystemProperties.CONSOLE_LOG_PATTERN))
.isEqualTo("console ${pid}");
} }
@Test @Test
@ -507,7 +514,7 @@ public class LoggingApplicationListenerTests {
"logging.file=target/${PID}.log"); "logging.file=target/${PID}.log");
this.initializer.initialize(this.context.getEnvironment(), this.initializer.initialize(this.context.getEnvironment(),
this.context.getClassLoader()); this.context.getClassLoader());
assertThat(System.getProperty("LOG_FILE")) assertThat(System.getProperty(LoggingSystemProperties.LOG_FILE))
.isEqualTo("target/" + new ApplicationPid().toString() + ".log"); .isEqualTo("target/" + new ApplicationPid().toString() + ".log");
} }

@ -53,8 +53,8 @@ public abstract class AbstractLoggingSystemTests {
@After @After
public void clear() { public void clear() {
System.clearProperty("LOG_FILE"); System.clearProperty(LoggingSystemProperties.LOG_FILE);
System.clearProperty("PID"); System.clearProperty(LoggingSystemProperties.PID_KEY);
} }
protected final String[] getSpringConfigLocations(AbstractLoggingSystem system) { protected final String[] getSpringConfigLocations(AbstractLoggingSystem system) {

@ -51,8 +51,9 @@ public class LogFileTests {
Properties properties = new Properties(); Properties properties = new Properties();
logFile.applyTo(properties); logFile.applyTo(properties);
assertThat(logFile.toString()).isEqualTo("log.file"); assertThat(logFile.toString()).isEqualTo("log.file");
assertThat(properties.getProperty("LOG_FILE")).isEqualTo("log.file"); assertThat(properties.getProperty(LoggingSystemProperties.LOG_FILE))
assertThat(properties.getProperty("LOG_PATH")).isNull(); .isEqualTo("log.file");
assertThat(properties.getProperty(LoggingSystemProperties.LOG_PATH)).isNull();
} }
@Test @Test
@ -62,8 +63,10 @@ public class LogFileTests {
Properties properties = new Properties(); Properties properties = new Properties();
logFile.applyTo(properties); logFile.applyTo(properties);
assertThat(logFile.toString()).isEqualTo("logpath/spring.log"); assertThat(logFile.toString()).isEqualTo("logpath/spring.log");
assertThat(properties.getProperty("LOG_FILE")).isEqualTo("logpath/spring.log"); assertThat(properties.getProperty(LoggingSystemProperties.LOG_FILE))
assertThat(properties.getProperty("LOG_PATH")).isEqualTo("logpath"); .isEqualTo("logpath/spring.log");
assertThat(properties.getProperty(LoggingSystemProperties.LOG_PATH))
.isEqualTo("logpath");
} }
@Test @Test
@ -73,8 +76,10 @@ public class LogFileTests {
Properties properties = new Properties(); Properties properties = new Properties();
logFile.applyTo(properties); logFile.applyTo(properties);
assertThat(logFile.toString()).isEqualTo("log.file"); assertThat(logFile.toString()).isEqualTo("log.file");
assertThat(properties.getProperty("LOG_FILE")).isEqualTo("log.file"); assertThat(properties.getProperty(LoggingSystemProperties.LOG_FILE))
assertThat(properties.getProperty("LOG_PATH")).isEqualTo("logpath"); .isEqualTo("log.file");
assertThat(properties.getProperty(LoggingSystemProperties.LOG_PATH))
.isEqualTo("logpath");
} }
private PropertyResolver getPropertyResolver(String file, String path) { private PropertyResolver getPropertyResolver(String file, String path) {

@ -34,6 +34,7 @@ import org.springframework.boot.logging.AbstractLoggingSystemTests;
import org.springframework.boot.logging.LogLevel; import org.springframework.boot.logging.LogLevel;
import org.springframework.boot.logging.LoggerConfiguration; import org.springframework.boot.logging.LoggerConfiguration;
import org.springframework.boot.logging.LoggingSystem; import org.springframework.boot.logging.LoggingSystem;
import org.springframework.boot.logging.LoggingSystemProperties;
import org.springframework.boot.testsupport.rule.OutputCapture; import org.springframework.boot.testsupport.rule.OutputCapture;
import org.springframework.util.ClassUtils; import org.springframework.util.ClassUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
@ -117,7 +118,7 @@ public class JavaLoggingSystemTests extends AbstractLoggingSystemTests {
@Test @Test
public void testSystemPropertyInitializesFormat() throws Exception { public void testSystemPropertyInitializesFormat() throws Exception {
System.setProperty("PID", "1234"); System.setProperty(LoggingSystemProperties.PID_KEY, "1234");
this.loggingSystem.beforeInitialize(); this.loggingSystem.beforeInitialize();
this.loggingSystem.initialize(null, "classpath:" + ClassUtils this.loggingSystem.initialize(null, "classpath:" + ClassUtils
.addResourcePathToPackagePath(getClass(), "logging.properties"), null); .addResourcePathToPackagePath(getClass(), "logging.properties"), null);

@ -41,6 +41,7 @@ import org.springframework.boot.logging.AbstractLoggingSystemTests;
import org.springframework.boot.logging.LogLevel; import org.springframework.boot.logging.LogLevel;
import org.springframework.boot.logging.LoggerConfiguration; import org.springframework.boot.logging.LoggerConfiguration;
import org.springframework.boot.logging.LoggingSystem; import org.springframework.boot.logging.LoggingSystem;
import org.springframework.boot.logging.LoggingSystemProperties;
import org.springframework.boot.testsupport.assertj.Matched; import org.springframework.boot.testsupport.assertj.Matched;
import org.springframework.boot.testsupport.rule.OutputCapture; import org.springframework.boot.testsupport.rule.OutputCapture;
import org.springframework.util.FileCopyUtils; import org.springframework.util.FileCopyUtils;
@ -257,7 +258,7 @@ public class Log4J2LoggingSystemTests extends AbstractLoggingSystemTests {
@Test @Test
public void customExceptionConversionWord() throws Exception { public void customExceptionConversionWord() throws Exception {
System.setProperty("LOG_EXCEPTION_CONVERSION_WORD", "%ex"); System.setProperty(LoggingSystemProperties.EXCEPTION_CONVERSION_WORD, "%ex");
try { try {
this.loggingSystem.beforeInitialize(); this.loggingSystem.beforeInitialize();
this.logger.info("Hidden"); this.logger.info("Hidden");
@ -273,7 +274,7 @@ public class Log4J2LoggingSystemTests extends AbstractLoggingSystemTests {
assertThat(fileContents).is(Matched.by(expectedOutput)); assertThat(fileContents).is(Matched.by(expectedOutput));
} }
finally { finally {
System.clearProperty("LOG_EXCEPTION_CONVERSION_WORD"); System.clearProperty(LoggingSystemProperties.EXCEPTION_CONVERSION_WORD);
} }
} }

@ -49,6 +49,7 @@ import org.springframework.boot.logging.LogLevel;
import org.springframework.boot.logging.LoggerConfiguration; import org.springframework.boot.logging.LoggerConfiguration;
import org.springframework.boot.logging.LoggingInitializationContext; import org.springframework.boot.logging.LoggingInitializationContext;
import org.springframework.boot.logging.LoggingSystem; import org.springframework.boot.logging.LoggingSystem;
import org.springframework.boot.logging.LoggingSystemProperties;
import org.springframework.boot.testsupport.assertj.Matched; import org.springframework.boot.testsupport.assertj.Matched;
import org.springframework.boot.testsupport.rule.OutputCapture; import org.springframework.boot.testsupport.rule.OutputCapture;
import org.springframework.mock.env.MockEnvironment; import org.springframework.mock.env.MockEnvironment;
@ -390,7 +391,7 @@ public class LogbackLoggingSystemTests extends AbstractLoggingSystemTests {
@Test @Test
public void customExceptionConversionWord() throws Exception { public void customExceptionConversionWord() throws Exception {
System.setProperty("LOG_EXCEPTION_CONVERSION_WORD", "%ex"); System.setProperty(LoggingSystemProperties.EXCEPTION_CONVERSION_WORD, "%ex");
try { try {
this.loggingSystem.beforeInitialize(); this.loggingSystem.beforeInitialize();
this.logger.info("Hidden"); this.logger.info("Hidden");
@ -407,7 +408,7 @@ public class LogbackLoggingSystemTests extends AbstractLoggingSystemTests {
assertThat(fileContents).is(Matched.by(expectedOutput)); assertThat(fileContents).is(Matched.by(expectedOutput));
} }
finally { finally {
System.clearProperty("LOG_EXCEPTION_CONVERSION_WORD"); System.clearProperty(LoggingSystemProperties.EXCEPTION_CONVERSION_WORD);
} }
} }
@ -419,7 +420,8 @@ public class LogbackLoggingSystemTests extends AbstractLoggingSystemTests {
LogFile logFile = getLogFile(tmpDir() + "/example.log", null, false); LogFile logFile = getLogFile(tmpDir() + "/example.log", null, false);
this.loggingSystem.initialize(this.initializationContext, this.loggingSystem.initialize(this.initializationContext,
"classpath:logback-nondefault.xml", logFile); "classpath:logback-nondefault.xml", logFile);
assertThat(System.getProperty("LOG_FILE")).endsWith("example.log"); assertThat(System.getProperty(LoggingSystemProperties.LOG_FILE))
.endsWith("example.log");
} }
@Test @Test

Loading…
Cancel
Save