From 21da4a5d12001bab202025fd81e9d33a3b57e488 Mon Sep 17 00:00:00 2001 From: TwinProduction Date: Sun, 4 Nov 2018 17:03:45 -0500 Subject: [PATCH 1/2] Rename logging.file to logging.file.name See gh-15089 --- .../LogFileWebEndpointAutoConfiguration.java | 22 +++++-- .../LogFileWebEndpointDocumentationTests.java | 2 +- ...FileWebEndpointAutoConfigurationTests.java | 17 +++++- .../actuate/logging/LogFileWebEndpoint.java | 4 +- .../logging/LogFileWebEndpointTests.java | 12 +++- ...LogFileWebEndpointWebIntegrationTests.java | 2 +- .../appendix-application-properties.adoc | 4 +- .../src/main/asciidoc/howto.adoc | 10 ++-- .../asciidoc/production-ready-features.adoc | 2 +- .../main/asciidoc/spring-boot-features.adoc | 8 +-- .../logging/LoggingApplicationListener.java | 4 +- .../springframework/boot/logging/LogFile.java | 35 ++++++++++-- ...itional-spring-configuration-metadata.json | 39 +++++++++---- .../SpringApplicationBuilderTests.java | 8 +-- .../LoggingApplicationListenerTests.java | 33 +++++++++-- .../boot/logging/LogFileTests.java | 57 +++++++++++++++++++ .../src/main/resources/application.properties | 2 +- 17 files changed, 212 insertions(+), 49 deletions(-) diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/logging/LogFileWebEndpointAutoConfiguration.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/logging/LogFileWebEndpointAutoConfiguration.java index 51c82fda5ea..dbb2c6326ee 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/logging/LogFileWebEndpointAutoConfiguration.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/logging/LogFileWebEndpointAutoConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-2018 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,6 +23,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionOutcome; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.SpringBootCondition; import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.boot.logging.LogFile; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ConditionContext; import org.springframework.context.annotation.Conditional; @@ -35,6 +36,7 @@ import org.springframework.util.StringUtils; * {@link EnableAutoConfiguration Auto-configuration} for {@link LogFileWebEndpoint}. * * @author Andy Wilkinson + * @author Christian Carriere-Tisseur * @since 2.0.0 */ @Configuration @@ -60,16 +62,26 @@ public class LogFileWebEndpointAutoConfiguration { public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) { Environment environment = context.getEnvironment(); - String config = environment.resolvePlaceholders("${logging.file:}"); + String config = environment + .resolvePlaceholders("${" + LogFile.FILE_NAME_PROPERTY + ":}"); + if (!StringUtils.hasText(config)) { + config = environment + .resolvePlaceholders("${" + LogFile.FILE_PROPERTY + ":}"); + } ConditionMessage.Builder message = ConditionMessage.forCondition("Log File"); if (StringUtils.hasText(config)) { return ConditionOutcome - .match(message.found("logging.file").items(config)); + .match(message.found(LogFile.FILE_NAME_PROPERTY).items(config)); + } + config = environment + .resolvePlaceholders("${" + LogFile.FILE_PATH_PROPERTY + ":}"); + if (!StringUtils.hasText(config)) { + config = environment + .resolvePlaceholders("${" + LogFile.PATH_PROPERTY + ":}"); } - config = environment.resolvePlaceholders("${logging.path:}"); if (StringUtils.hasText(config)) { return ConditionOutcome - .match(message.found("logging.path").items(config)); + .match(message.found(LogFile.FILE_PATH_PROPERTY).items(config)); } config = environment.getProperty("management.endpoint.logfile.external-file"); if (StringUtils.hasText(config)) { diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/endpoint/web/documentation/LogFileWebEndpointDocumentationTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/endpoint/web/documentation/LogFileWebEndpointDocumentationTests.java index dd0c0ed71c1..3ef69af3e5e 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/endpoint/web/documentation/LogFileWebEndpointDocumentationTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/endpoint/web/documentation/LogFileWebEndpointDocumentationTests.java @@ -34,7 +34,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. * * @author Andy Wilkinson */ -@TestPropertySource(properties = "logging.file=src/test/resources/org/springframework/boot/actuate/autoconfigure/endpoint/web/documentation/sample.log") +@TestPropertySource(properties = "logging.file.name=src/test/resources/org/springframework/boot/actuate/autoconfigure/endpoint/web/documentation/sample.log") public class LogFileWebEndpointDocumentationTests extends MockMvcEndpointDocumentationTests { diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/logging/LogFileWebEndpointAutoConfigurationTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/logging/LogFileWebEndpointAutoConfigurationTests.java index f426ad24b3f..c36cb90104f 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/logging/LogFileWebEndpointAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/logging/LogFileWebEndpointAutoConfigurationTests.java @@ -38,6 +38,7 @@ import static org.assertj.core.api.Assertions.assertThat; * @author Andy Wilkinson * @author Stephane Nicoll * @author Phillip Webb + * @author Christian Carriere-Tisseur */ public class LogFileWebEndpointAutoConfigurationTests { @@ -49,12 +50,26 @@ public class LogFileWebEndpointAutoConfigurationTests { @Test public void logFileWebEndpointIsAutoConfiguredWhenLoggingFileIsSet() { + this.contextRunner.withPropertyValues("logging.file.name:test.log").run( + (context) -> assertThat(context).hasSingleBean(LogFileWebEndpoint.class)); + } + + @Test + @Deprecated + public void logFileWebEndpointIsAutoConfiguredWhenLoggingFileIsSetWithDeprecatedProperty() { this.contextRunner.withPropertyValues("logging.file:test.log").run( (context) -> assertThat(context).hasSingleBean(LogFileWebEndpoint.class)); } @Test public void logFileWebEndpointIsAutoConfiguredWhenLoggingPathIsSet() { + this.contextRunner.withPropertyValues("logging.file.path:test/logs").run( + (context) -> assertThat(context).hasSingleBean(LogFileWebEndpoint.class)); + } + + @Test + @Deprecated + public void logFileWebEndpointIsAutoConfiguredWhenLoggingPathIsSetWithDeprecatedProperty() { this.contextRunner.withPropertyValues("logging.path:test/logs").run( (context) -> assertThat(context).hasSingleBean(LogFileWebEndpoint.class)); } @@ -71,7 +86,7 @@ public class LogFileWebEndpointAutoConfigurationTests { @Test public void logFileWebEndpointCanBeDisabled() { this.contextRunner - .withPropertyValues("logging.file:test.log", + .withPropertyValues("logging.file.name:test.log", "management.endpoint.logfile.enabled:false") .run((context) -> assertThat(context) .hasSingleBean(LogFileWebEndpoint.class)); diff --git a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/logging/LogFileWebEndpoint.java b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/logging/LogFileWebEndpoint.java index d0e00c66cc3..3be400b783e 100644 --- a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/logging/LogFileWebEndpoint.java +++ b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/logging/LogFileWebEndpoint.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-2018 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. @@ -70,7 +70,7 @@ public class LogFileWebEndpoint { } LogFile logFile = LogFile.get(this.environment); if (logFile == null) { - logger.debug("Missing 'logging.file' or 'logging.path' properties"); + logger.debug("Missing 'logging.file.name' or 'logging.file.path' properties"); return null; } return new FileSystemResource(logFile.toString()); diff --git a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/logging/LogFileWebEndpointTests.java b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/logging/LogFileWebEndpointTests.java index 22d27da724d..fd008fc1faf 100644 --- a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/logging/LogFileWebEndpointTests.java +++ b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/logging/LogFileWebEndpointTests.java @@ -63,12 +63,22 @@ public class LogFileWebEndpointTests { @Test public void nullResponseWithMissingLogFile() { - this.environment.setProperty("logging.file", "no_test.log"); + this.environment.setProperty("logging.file.name", "no_test.log"); assertThat(this.endpoint.logFile()).isNull(); } @Test public void resourceResponseWithLogFile() throws Exception { + this.environment.setProperty("logging.file.name", this.logFile.getAbsolutePath()); + Resource resource = this.endpoint.logFile(); + assertThat(resource).isNotNull(); + assertThat(StreamUtils.copyToString(resource.getInputStream(), + StandardCharsets.UTF_8)).isEqualTo("--TEST--"); + } + + @Test + @Deprecated + public void resourceResponseWithLogFileAndDeprecatedProperty() throws Exception { this.environment.setProperty("logging.file", this.logFile.getAbsolutePath()); Resource resource = this.endpoint.logFile(); assertThat(resource).isNotNull(); diff --git a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/logging/LogFileWebEndpointWebIntegrationTests.java b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/logging/LogFileWebEndpointWebIntegrationTests.java index 7c713c3610b..6cd9f4f22cd 100644 --- a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/logging/LogFileWebEndpointWebIntegrationTests.java +++ b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/logging/LogFileWebEndpointWebIntegrationTests.java @@ -65,7 +65,7 @@ public class LogFileWebEndpointWebIntegrationTests { @Test public void getRequestProducesResponseWithLogFile() { - TestPropertyValues.of("logging.file:" + this.logFile.getAbsolutePath()) + TestPropertyValues.of("logging.file.name:" + this.logFile.getAbsolutePath()) .applyTo(context); client.get().uri("/actuator/logfile").exchange().expectStatus().isOk() .expectBody(String.class).isEqualTo("--TEST--"); diff --git a/spring-boot-project/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc b/spring-boot-project/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc index 04e16706631..af6f1f35b93 100644 --- a/spring-boot-project/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc +++ b/spring-boot-project/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc @@ -37,12 +37,12 @@ content into your application. Rather, pick only the properties that you need. # LOGGING logging.config= # Location of the logging configuration file. For instance, `classpath:logback.xml` for Logback. logging.exception-conversion-word=%wEx # Conversion word used when logging exceptions. - logging.file= # Log file name (for instance, `myapp.log`). Names can be an exact location or relative to the current directory. + logging.file.name= # Log file name (for instance, `myapp.log`). Names can be an exact location or relative to the current directory. logging.file.max-history=0 # Maximum of archive log files to keep. Only supported with the default logback setup. logging.file.max-size=10MB # Maximum log file size. Only supported with the default logback setup. logging.group.*= # Log groups to quickly change multiple loggers at the same time. For instance, `logging.level.db=org.hibernate,org.springframework.jdbc`. logging.level.*= # Log levels severity mapping. For instance, `logging.level.org.springframework=DEBUG`. - logging.path= # Location of the log file. For instance, `/var/log`. + logging.file.path= # Location of the log file. For instance, `/var/log`. logging.pattern.console= # Appender pattern for output to the console. Supported only with the default Logback setup. logging.pattern.dateformat=yyyy-MM-dd HH:mm:ss.SSS # Appender pattern for log date format. Supported only with the default Logback setup. logging.pattern.file= # Appender pattern for output to a file. Supported only with the default Logback setup. diff --git a/spring-boot-project/spring-boot-docs/src/main/asciidoc/howto.adoc b/spring-boot-project/spring-boot-docs/src/main/asciidoc/howto.adoc index 6e0992081ca..d718cca7ad6 100644 --- a/spring-boot-project/spring-boot-docs/src/main/asciidoc/howto.adoc +++ b/spring-boot-project/spring-boot-docs/src/main/asciidoc/howto.adoc @@ -1518,7 +1518,7 @@ in the following example: ---- You can also set the location of a file to which to write the log (in addition to the -console) by using "logging.file". +console) by using "logging.file.name". To configure the more fine-grained settings of a logging system, you need to use the native configuration format supported by the `LoggingSystem` in question. By default, Spring Boot @@ -1548,8 +1548,8 @@ If you look at `base.xml` in the spring-boot jar, you can see that it uses some useful System properties that the `LoggingSystem` takes care of creating for you: * `${PID}`: The current process ID. -* `${LOG_FILE}`: Whether `logging.file` was set in Boot's external configuration. -* `${LOG_PATH}`: Whether `logging.path` (representing a directory for +* `${LOG_FILE}`: Whether `logging.file.name` was set in Boot's external configuration. +* `${LOG_PATH}`: Whether `logging.file.path` (representing a directory for log files to live in) was set in Boot's external configuration. * `${LOG_EXCEPTION_CONVERSION_WORD}`: Whether `logging.exception-conversion-word` was set in Boot's external configuration. @@ -1582,12 +1582,12 @@ shown in the following example: ---- -You also need to add `logging.file` to your `application.properties`, as shown in the +You also need to add `logging.file.name` to your `application.properties`, as shown in the following example: [source,properties,indent=0,subs="verbatim,quotes,attributes"] ---- - logging.file=myapplication.log + logging.file.name=myapplication.log ---- diff --git a/spring-boot-project/spring-boot-docs/src/main/asciidoc/production-ready-features.adoc b/spring-boot-project/spring-boot-docs/src/main/asciidoc/production-ready-features.adoc index 31b1cfc8110..4ca41939f96 100644 --- a/spring-boot-project/spring-boot-docs/src/main/asciidoc/production-ready-features.adoc +++ b/spring-boot-project/spring-boot-docs/src/main/asciidoc/production-ready-features.adoc @@ -164,7 +164,7 @@ use the following additional endpoints: |Yes |`logfile` -|Returns the contents of the logfile (if `logging.file` or `logging.path` properties have +|Returns the contents of the logfile (if `logging.file.name` or `logging.file.path` properties have been set). Supports the use of the HTTP `Range` header to retrieve part of the log file's content. |Yes diff --git a/spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc b/spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc index 978c91b704f..d170c3c1c2f 100644 --- a/spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc +++ b/spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc @@ -1686,7 +1686,7 @@ The following colors and styles are supported: === File Output By default, Spring Boot logs only to the console and does not write log files. If you want to write log files in addition to the console output, you need to set a -`logging.file` or `logging.path` property (for example, in your +`logging.file.name` or `logging.file.path` property (for example, in your `application.properties`). The following table shows how the `logging.*` properties can be used together: @@ -1694,7 +1694,7 @@ The following table shows how the `logging.*` properties can be used together: .Logging properties [cols="1,1,1,4"] |=== -|`logging.file` |`logging.path` |Example |Description +|`logging.file.name` |`logging.file.path` |Example |Description |_(none)_ |_(none)_ @@ -1835,7 +1835,7 @@ To help with the customization, some other properties are transferred from the S |`LOG_EXCEPTION_CONVERSION_WORD` |The conversion word used when logging exceptions. -|`logging.file` +|`logging.file.name` |`LOG_FILE` |If defined, it is used in the default log configuration. @@ -1849,7 +1849,7 @@ setup.) |Maximum number of archive log files to keep (if LOG_FILE enabled). (Only supported with the default Logback setup.) -|`logging.path` +|`logging.file.path` |`LOG_PATH` |If defined, it is used in the default log configuration. diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/logging/LoggingApplicationListener.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/logging/LoggingApplicationListener.java index ac2854b9dd7..59277ba2ec7 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/logging/LoggingApplicationListener.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/logging/LoggingApplicationListener.java @@ -68,8 +68,8 @@ import org.springframework.util.StringUtils; * {@literal java -jar myapp.jar [--debug | --trace]}). If you prefer to ignore these * properties you can set {@link #setParseArgs(boolean) parseArgs} to {@code false}. *

- * By default, log output is only written to the console. If a log file is required the - * {@code logging.path} and {@code logging.file} properties can be used. + * By default, log output is only written to the console. If a log file is required, the + * {@code logging.file.path} and {@code logging.file.name} properties can be used. *

* Some system properties may be set as side effects, and these can be useful if the * logging configuration supports placeholders (i.e. log4j or logback): 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 39d8f050d6f..e5f341741a9 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 @@ -25,11 +25,12 @@ import org.springframework.util.StringUtils; /** * A reference to a log output file. Log output files are specified using - * {@code logging.file} or {@code logging.path} {@link Environment} properties. If the - * {@code logging.file} property is not specified {@code "spring.log"} will be written in - * the {@code logging.path} directory. + * {@code logging.file.name} or {@code logging.file.path} {@link Environment} properties. + * If the {@code logging.file.name} property is not specified {@code "spring.log"} will be + * written in the {@code logging.file.path} directory. * * @author Phillip Webb + * @author Christian Carriere-Tisseur * @since 1.2.1 * @see #get(PropertyResolver) */ @@ -38,15 +39,33 @@ public class LogFile { /** * The name of the Spring property that contains the name of the log file. Names can * be an exact location or relative to the current directory. + * @deprecated since 2.2.0 in favor of {@link #FILE_NAME_PROPERTY} */ + @Deprecated public static final String FILE_PROPERTY = "logging.file"; /** * The name of the Spring property that contains the directory where log files are * written. + * @deprecated since 2.2.0 in favor of {@link #FILE_PATH_PROPERTY} */ + @Deprecated public static final String PATH_PROPERTY = "logging.path"; + /** + * The name of the Spring property that contains the name of the log file. Names can + * be an exact location or relative to the current directory. + * @since 2.2.0 + */ + public static final String FILE_NAME_PROPERTY = "logging.file.name"; + + /** + * The name of the Spring property that contains the directory where log files are + * written. + * @since 2.2.0 + */ + public static final String FILE_PATH_PROPERTY = "logging.file.path"; + private final String file; private final String path; @@ -113,8 +132,14 @@ public class LogFile { * suitable properties */ public static LogFile get(PropertyResolver propertyResolver) { - String file = propertyResolver.getProperty(FILE_PROPERTY); - String path = propertyResolver.getProperty(PATH_PROPERTY); + String file = propertyResolver.getProperty(FILE_NAME_PROPERTY); + String path = propertyResolver.getProperty(FILE_PATH_PROPERTY); + if (file == null) { + file = propertyResolver.getProperty(FILE_PROPERTY); + } + if (path == null) { + path = propertyResolver.getProperty(PATH_PROPERTY); + } if (StringUtils.hasLength(file) || StringUtils.hasLength(path)) { return new LogFile(file, path); } 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 3a7236be436..56759ba416b 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 @@ -68,11 +68,17 @@ "sourceType": "org.springframework.boot.context.logging.LoggingApplicationListener" }, { - "name": "logging.file", + "name": "logging.file.name", "type": "java.lang.String", "description": "Log file name (for instance, `myapp.log`). Names can be an exact location or relative to the current directory.", "sourceType": "org.springframework.boot.context.logging.LoggingApplicationListener" }, + { + "name": "logging.file.path", + "type": "java.lang.String", + "description": "Location of the log file. For instance, `/var/log`.", + "sourceType": "org.springframework.boot.context.logging.LoggingApplicationListener" + }, { "name": "logging.file.max-size", "type": "java.lang.String", @@ -99,12 +105,6 @@ "description": "Log levels severity mapping. For instance, `logging.level.org.springframework=DEBUG`.", "sourceType": "org.springframework.boot.context.logging.LoggingApplicationListener" }, - { - "name": "logging.path", - "type": "java.lang.String", - "description": "Location of the log file. For instance, `/var/log`.", - "sourceType": "org.springframework.boot.context.logging.LoggingApplicationListener" - }, { "name": "logging.pattern.console", "type": "java.lang.String", @@ -332,8 +332,28 @@ "type": "java.lang.Integer", "description": "Application index.", "deprecation": { - "level": "error", - "reason": "Application context ids are now unique by default." + "reason": "Application context ids are now unique by default.", + "level": "error" + } + }, + { + "name": "logging.file", + "type": "java.lang.String", + "description": "Log file name (for instance, `myapp.log`). Names can be an exact location or relative to the current directory.", + "sourceType": "org.springframework.boot.context.logging.LoggingApplicationListener", + "deprecation": { + "replacement": "logging.file.name", + "level": "error" + } + }, + { + "name": "logging.path", + "type": "java.lang.String", + "description": "Location of the log file. For instance, `/var/log`.", + "sourceType": "org.springframework.boot.context.logging.LoggingApplicationListener", + "deprecation": { + "replacement": "logging.file.path", + "level": "error" } } ], @@ -346,7 +366,6 @@ "parameters": { "group": false } - } ] }, diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/builder/SpringApplicationBuilderTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/builder/SpringApplicationBuilderTests.java index 2ee328983e4..bd75665736e 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/builder/SpringApplicationBuilderTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/builder/SpringApplicationBuilderTests.java @@ -100,13 +100,13 @@ public class SpringApplicationBuilderTests { public void propertiesWithRepeatSeparator() { SpringApplicationBuilder application = new SpringApplicationBuilder() .sources(ExampleConfig.class).contextClass(StaticApplicationContext.class) - .properties("one=c:\\logging.file", "two=a:b", "three:c:\\logging.file", - "four:a:b"); + .properties("one=c:\\logging.file.name", "two=a:b", + "three:c:\\logging.file.name", "four:a:b"); this.context = application.run(); ConfigurableEnvironment environment = this.context.getEnvironment(); - assertThat(environment.getProperty("one")).isEqualTo("c:\\logging.file"); + assertThat(environment.getProperty("one")).isEqualTo("c:\\logging.file.name"); assertThat(environment.getProperty("two")).isEqualTo("a:b"); - assertThat(environment.getProperty("three")).isEqualTo("c:\\logging.file"); + assertThat(environment.getProperty("three")).isEqualTo("c:\\logging.file.name"); assertThat(environment.getProperty("four")).isEqualTo("a:b"); } diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/logging/LoggingApplicationListenerTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/logging/LoggingApplicationListenerTests.java index 8f5cafa3ac4..4bf4b586325 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 @@ -213,7 +213,7 @@ public class LoggingApplicationListenerTests { public void addLogFileProperty() { TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context, "logging.config=classpath:logback-nondefault.xml", - "logging.file=target/foo.log"); + "logging.file.name=target/foo.log"); this.initializer.initialize(this.context.getEnvironment(), this.context.getClassLoader()); Log logger = LogFactory.getLog(LoggingApplicationListenerTests.class); @@ -228,7 +228,7 @@ public class LoggingApplicationListenerTests { public void addLogFilePropertyWithDefault() { assertThat(new File("target/foo.log").exists()).isFalse(); TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context, - "logging.file=target/foo.log"); + "logging.file.name=target/foo.log"); this.initializer.initialize(this.context.getEnvironment(), this.context.getClassLoader()); Log logger = LogFactory.getLog(LoggingApplicationListenerTests.class); @@ -240,7 +240,7 @@ public class LoggingApplicationListenerTests { public void addLogPathProperty() { TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context, "logging.config=classpath:logback-nondefault.xml", - "logging.path=target/foo/"); + "logging.file.path=target/foo/"); this.initializer.initialize(this.context.getEnvironment(), this.context.getClassLoader()); Log logger = LogFactory.getLog(LoggingApplicationListenerTests.class); @@ -490,6 +490,31 @@ public class LoggingApplicationListenerTests { @Test public void systemPropertiesAreSetForLoggingConfiguration() { + TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context, + "logging.exception-conversion-word=conversion", + "logging.file.name=target/log", "logging.file.path=path", + "logging.pattern.console=console", "logging.pattern.file=file", + "logging.pattern.level=level"); + this.initializer.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("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 + @Deprecated + public void systemPropertiesAreSetForLoggingConfigurationWithDeprecatedProperties() { TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context, "logging.exception-conversion-word=conversion", "logging.file=target/log", "logging.path=path", "logging.pattern.console=console", @@ -536,7 +561,7 @@ public class LoggingApplicationListenerTests { @Test public void logFilePropertiesCanReferenceSystemProperties() { TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context, - "logging.file=target/${PID}.log"); + "logging.file.name=target/${PID}.log"); this.initializer.initialize(this.context.getEnvironment(), this.context.getClassLoader()); assertThat(System.getProperty(LoggingSystemProperties.LOG_FILE)) 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 f99727db45c..197fe0d5ca7 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 @@ -56,6 +56,20 @@ public class LogFileTests { assertThat(properties.getProperty(LoggingSystemProperties.LOG_PATH)).isNull(); } + @Test + @Deprecated + public void loggingFileWithDeprecatedProperties() { + PropertyResolver resolver = getPropertyResolverWithDeprecatedProperties( + "log.file", null); + LogFile logFile = LogFile.get(resolver); + Properties properties = new Properties(); + logFile.applyTo(properties); + assertThat(logFile.toString()).isEqualTo("log.file"); + assertThat(properties.getProperty(LoggingSystemProperties.LOG_FILE)) + .isEqualTo("log.file"); + assertThat(properties.getProperty(LoggingSystemProperties.LOG_PATH)).isNull(); + } + @Test public void loggingPath() { PropertyResolver resolver = getPropertyResolver(null, "logpath"); @@ -69,6 +83,21 @@ public class LogFileTests { .isEqualTo("logpath"); } + @Test + @Deprecated + public void loggingPathWithDeprecatedProperties() { + PropertyResolver resolver = getPropertyResolverWithDeprecatedProperties(null, + "logpath"); + LogFile logFile = LogFile.get(resolver); + Properties properties = new Properties(); + logFile.applyTo(properties); + assertThat(logFile.toString()).isEqualTo("logpath/spring.log"); + assertThat(properties.getProperty(LoggingSystemProperties.LOG_FILE)) + .isEqualTo("logpath/spring.log"); + assertThat(properties.getProperty(LoggingSystemProperties.LOG_PATH)) + .isEqualTo("logpath"); + } + @Test public void loggingFileAndPath() { PropertyResolver resolver = getPropertyResolver("log.file", "logpath"); @@ -82,8 +111,36 @@ public class LogFileTests { .isEqualTo("logpath"); } + @Test + @Deprecated + public void loggingFileAndPathWithDeprecatedProperties() { + PropertyResolver resolver = getPropertyResolverWithDeprecatedProperties( + "log.file", "logpath"); + LogFile logFile = LogFile.get(resolver); + Properties properties = new Properties(); + logFile.applyTo(properties); + assertThat(logFile.toString()).isEqualTo("log.file"); + assertThat(properties.getProperty(LoggingSystemProperties.LOG_FILE)) + .isEqualTo("log.file"); + assertThat(properties.getProperty(LoggingSystemProperties.LOG_PATH)) + .isEqualTo("logpath"); + } + private PropertyResolver getPropertyResolver(String file, String path) { Map properties = new LinkedHashMap<>(); + properties.put("logging.file.name", file); + properties.put("logging.file.path", path); + PropertySource propertySource = new MapPropertySource("properties", + properties); + MutablePropertySources propertySources = new MutablePropertySources(); + propertySources.addFirst(propertySource); + return new PropertySourcesPropertyResolver(propertySources); + } + + @Deprecated + private PropertyResolver getPropertyResolverWithDeprecatedProperties(String file, + String path) { + Map properties = new LinkedHashMap<>(); properties.put("logging.file", file); properties.put("logging.path", path); PropertySource propertySource = new MapPropertySource("properties", diff --git a/spring-boot-samples/spring-boot-sample-actuator/src/main/resources/application.properties b/spring-boot-samples/spring-boot-sample-actuator/src/main/resources/application.properties index bfc6809e10a..09413b04851 100644 --- a/spring-boot-samples/spring-boot-sample-actuator/src/main/resources/application.properties +++ b/spring-boot-samples/spring-boot-sample-actuator/src/main/resources/application.properties @@ -3,7 +3,7 @@ service.name=Phil spring.security.user.name=user spring.security.user.password=password -# logging.file=/tmp/logs/app.log +# logging.file.name=/tmp/logs/app.log # logging.level.org.springframework.security=DEBUG management.server.address=127.0.0.1 From 7939b8b4da247fc3984cb253079372a8fbc49edb Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Mon, 3 Dec 2018 10:32:18 +0100 Subject: [PATCH 2/2] Polish "Rename logging.file to logging.file.name" Closes gh-15089 --- .../LogFileWebEndpointAutoConfiguration.java | 26 +++--- .../logging/LogFileWebEndpointTests.java | 2 +- ...LogFileWebEndpointWebIntegrationTests.java | 2 +- .../appendix-application-properties.adoc | 4 +- .../asciidoc/production-ready-features.adoc | 6 +- .../springframework/boot/logging/LogFile.java | 24 ++++-- ...itional-spring-configuration-metadata.json | 43 +++++----- .../LoggingApplicationListenerTests.java | 57 +++++++++--- .../boot/logging/LogFileTests.java | 86 ++++++++----------- 9 files changed, 137 insertions(+), 113 deletions(-) diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/logging/LogFileWebEndpointAutoConfiguration.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/logging/LogFileWebEndpointAutoConfiguration.java index dbb2c6326ee..54b0226bf35 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/logging/LogFileWebEndpointAutoConfiguration.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/logging/LogFileWebEndpointAutoConfiguration.java @@ -58,27 +58,20 @@ public class LogFileWebEndpointAutoConfiguration { private static class LogFileCondition extends SpringBootCondition { + @SuppressWarnings("deprecation") @Override public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) { Environment environment = context.getEnvironment(); - String config = environment - .resolvePlaceholders("${" + LogFile.FILE_NAME_PROPERTY + ":}"); - if (!StringUtils.hasText(config)) { - config = environment - .resolvePlaceholders("${" + LogFile.FILE_PROPERTY + ":}"); - } + String config = getLogFileConfig(environment, LogFile.FILE_NAME_PROPERTY, + LogFile.FILE_PROPERTY); ConditionMessage.Builder message = ConditionMessage.forCondition("Log File"); if (StringUtils.hasText(config)) { return ConditionOutcome .match(message.found(LogFile.FILE_NAME_PROPERTY).items(config)); } - config = environment - .resolvePlaceholders("${" + LogFile.FILE_PATH_PROPERTY + ":}"); - if (!StringUtils.hasText(config)) { - config = environment - .resolvePlaceholders("${" + LogFile.PATH_PROPERTY + ":}"); - } + config = getLogFileConfig(environment, LogFile.FILE_PATH_PROPERTY, + LogFile.PATH_PROPERTY); if (StringUtils.hasText(config)) { return ConditionOutcome .match(message.found(LogFile.FILE_PATH_PROPERTY).items(config)); @@ -92,6 +85,15 @@ public class LogFileWebEndpointAutoConfiguration { return ConditionOutcome.noMatch(message.didNotFind("logging file").atAll()); } + private String getLogFileConfig(Environment environment, String configName, + String deprecatedConfigName) { + String config = environment.resolvePlaceholders("${" + configName + ":}"); + if (StringUtils.hasText(config)) { + return config; + } + return environment.resolvePlaceholders("${" + deprecatedConfigName + ":}"); + } + } } diff --git a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/logging/LogFileWebEndpointTests.java b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/logging/LogFileWebEndpointTests.java index fd008fc1faf..4e8c6ecaf66 100644 --- a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/logging/LogFileWebEndpointTests.java +++ b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/logging/LogFileWebEndpointTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-2018 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. diff --git a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/logging/LogFileWebEndpointWebIntegrationTests.java b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/logging/LogFileWebEndpointWebIntegrationTests.java index 6cd9f4f22cd..7ab47e7c86a 100644 --- a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/logging/LogFileWebEndpointWebIntegrationTests.java +++ b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/logging/LogFileWebEndpointWebIntegrationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-2018 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. diff --git a/spring-boot-project/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc b/spring-boot-project/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc index af6f1f35b93..b1b0eedf422 100644 --- a/spring-boot-project/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc +++ b/spring-boot-project/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc @@ -37,12 +37,12 @@ content into your application. Rather, pick only the properties that you need. # LOGGING logging.config= # Location of the logging configuration file. For instance, `classpath:logback.xml` for Logback. logging.exception-conversion-word=%wEx # Conversion word used when logging exceptions. - logging.file.name= # Log file name (for instance, `myapp.log`). Names can be an exact location or relative to the current directory. logging.file.max-history=0 # Maximum of archive log files to keep. Only supported with the default logback setup. logging.file.max-size=10MB # Maximum log file size. Only supported with the default logback setup. + logging.file.name= # Log file name (for instance, `myapp.log`). Names can be an exact location or relative to the current directory. + logging.file.path= # Location of the log file. For instance, `/var/log`. logging.group.*= # Log groups to quickly change multiple loggers at the same time. For instance, `logging.level.db=org.hibernate,org.springframework.jdbc`. logging.level.*= # Log levels severity mapping. For instance, `logging.level.org.springframework=DEBUG`. - logging.file.path= # Location of the log file. For instance, `/var/log`. logging.pattern.console= # Appender pattern for output to the console. Supported only with the default Logback setup. logging.pattern.dateformat=yyyy-MM-dd HH:mm:ss.SSS # Appender pattern for log date format. Supported only with the default Logback setup. logging.pattern.file= # Appender pattern for output to a file. Supported only with the default Logback setup. diff --git a/spring-boot-project/spring-boot-docs/src/main/asciidoc/production-ready-features.adoc b/spring-boot-project/spring-boot-docs/src/main/asciidoc/production-ready-features.adoc index 4ca41939f96..5053592ffe5 100644 --- a/spring-boot-project/spring-boot-docs/src/main/asciidoc/production-ready-features.adoc +++ b/spring-boot-project/spring-boot-docs/src/main/asciidoc/production-ready-features.adoc @@ -164,9 +164,9 @@ use the following additional endpoints: |Yes |`logfile` -|Returns the contents of the logfile (if `logging.file.name` or `logging.file.path` properties have -been set). Supports the use of the HTTP `Range` header to retrieve part of the log file's -content. +|Returns the contents of the logfile (if `logging.file.name` or `logging.file.path` +properties have been set). Supports the use of the HTTP `Range` header to retrieve part of +the log file's content. |Yes |`prometheus` 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 e5f341741a9..c99d443fe66 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-2017 the original author or authors. + * Copyright 2012-2018 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. @@ -131,19 +131,25 @@ public class LogFile { * @return a {@link LogFile} or {@code null} if the environment didn't contain any * suitable properties */ + @SuppressWarnings("deprecation") public static LogFile get(PropertyResolver propertyResolver) { - String file = propertyResolver.getProperty(FILE_NAME_PROPERTY); - String path = propertyResolver.getProperty(FILE_PATH_PROPERTY); - if (file == null) { - file = propertyResolver.getProperty(FILE_PROPERTY); - } - if (path == null) { - path = propertyResolver.getProperty(PATH_PROPERTY); - } + String file = getLogFileProperty(propertyResolver, FILE_NAME_PROPERTY, + FILE_PROPERTY); + String path = getLogFileProperty(propertyResolver, FILE_PATH_PROPERTY, + PATH_PROPERTY); if (StringUtils.hasLength(file) || StringUtils.hasLength(path)) { return new LogFile(file, path); } return null; } + private static String getLogFileProperty(PropertyResolver propertyResolver, + String propertyName, String deprecatedPropertyName) { + String property = propertyResolver.getProperty(propertyName); + if (property != null) { + return property; + } + return propertyResolver.getProperty(deprecatedPropertyName); + } + } 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 56759ba416b..dccde53dda6 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 @@ -67,6 +67,15 @@ "defaultValue": "%wEx", "sourceType": "org.springframework.boot.context.logging.LoggingApplicationListener" }, + { + "name": "logging.file", + "type": "java.lang.String", + "description": "Log file name (for instance, `myapp.log`). Names can be an exact location or relative to the current directory.", + "sourceType": "org.springframework.boot.context.logging.LoggingApplicationListener", + "deprecation": { + "replacement": "logging.file.name" + } + }, { "name": "logging.file.name", "type": "java.lang.String", @@ -105,6 +114,15 @@ "description": "Log levels severity mapping. For instance, `logging.level.org.springframework=DEBUG`.", "sourceType": "org.springframework.boot.context.logging.LoggingApplicationListener" }, + { + "name": "logging.path", + "type": "java.lang.String", + "description": "Location of the log file. For instance, `/var/log`.", + "sourceType": "org.springframework.boot.context.logging.LoggingApplicationListener", + "deprecation": { + "replacement": "logging.file.path" + } + }, { "name": "logging.pattern.console", "type": "java.lang.String", @@ -332,28 +350,8 @@ "type": "java.lang.Integer", "description": "Application index.", "deprecation": { - "reason": "Application context ids are now unique by default.", - "level": "error" - } - }, - { - "name": "logging.file", - "type": "java.lang.String", - "description": "Log file name (for instance, `myapp.log`). Names can be an exact location or relative to the current directory.", - "sourceType": "org.springframework.boot.context.logging.LoggingApplicationListener", - "deprecation": { - "replacement": "logging.file.name", - "level": "error" - } - }, - { - "name": "logging.path", - "type": "java.lang.String", - "description": "Location of the log file. For instance, `/var/log`.", - "sourceType": "org.springframework.boot.context.logging.LoggingApplicationListener", - "deprecation": { - "replacement": "logging.file.path", - "level": "error" + "level": "error", + "reason": "Application context ids are now unique by default." } } ], @@ -366,6 +364,7 @@ "parameters": { "group": false } + } ] }, 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 4bf4b586325..d566e8d40c9 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 @@ -224,6 +224,22 @@ public class LoggingApplicationListenerTests { assertThat(output).startsWith("target/foo.log"); } + @Test + @Deprecated + public void addLogFilePropertyWithDeprecatedProperty() { + TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context, + "logging.config=classpath:logback-nondefault.xml", + "logging.file=target/foo.log"); + this.initializer.initialize(this.context.getEnvironment(), + this.context.getClassLoader()); + Log logger = LogFactory.getLog(LoggingApplicationListenerTests.class); + String existingOutput = this.outputCapture.toString(); + logger.info("Hello world"); + String output = this.outputCapture.toString().substring(existingOutput.length()) + .trim(); + assertThat(output).startsWith("target/foo.log"); + } + @Test public void addLogFilePropertyWithDefault() { assertThat(new File("target/foo.log").exists()).isFalse(); @@ -236,6 +252,19 @@ public class LoggingApplicationListenerTests { assertThat(new File("target/foo.log").exists()).isTrue(); } + @Test + @Deprecated + public void addLogFilePropertyWithDefaultAndDeprecatedProperty() { + assertThat(new File("target/foo.log").exists()).isFalse(); + TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context, + "logging.file=target/foo.log"); + this.initializer.initialize(this.context.getEnvironment(), + this.context.getClassLoader()); + Log logger = LogFactory.getLog(LoggingApplicationListenerTests.class); + logger.info("Hello world"); + assertThat(new File("target/foo.log").exists()).isTrue(); + } + @Test public void addLogPathProperty() { TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context, @@ -251,6 +280,21 @@ public class LoggingApplicationListenerTests { assertThat(output).startsWith("target/foo/spring.log"); } + @Test + public void addLogPathPropertyWithDeprecatedProperty() { + TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context, + "logging.config=classpath:logback-nondefault.xml", + "logging.path=target/foo/"); + this.initializer.initialize(this.context.getEnvironment(), + this.context.getClassLoader()); + Log logger = LogFactory.getLog(LoggingApplicationListenerTests.class); + String existingOutput = this.outputCapture.toString(); + logger.info("Hello world"); + String output = this.outputCapture.toString().substring(existingOutput.length()) + .trim(); + assertThat(output).startsWith("target/foo/spring.log"); + } + @Test public void parseDebugArg() { TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context, "debug"); @@ -516,24 +560,13 @@ public class LoggingApplicationListenerTests { @Deprecated public void systemPropertiesAreSetForLoggingConfigurationWithDeprecatedProperties() { TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context, - "logging.exception-conversion-word=conversion", "logging.file=target/log", - "logging.path=path", "logging.pattern.console=console", - "logging.pattern.file=file", "logging.pattern.level=level"); + "logging.file=target/log", "logging.path=path"); this.initializer.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("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 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 197fe0d5ca7..81c3c647407 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 @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-2018 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. @@ -16,6 +16,7 @@ package org.springframework.boot.logging; +import java.util.Collections; import java.util.LinkedHashMap; import java.util.Map; import java.util.Properties; @@ -39,28 +40,27 @@ public class LogFileTests { @Test public void noProperties() { - PropertyResolver resolver = getPropertyResolver(null, null); + PropertyResolver resolver = getPropertyResolver(Collections.emptyMap()); LogFile logFile = LogFile.get(resolver); assertThat(logFile).isNull(); } @Test public void loggingFile() { - PropertyResolver resolver = getPropertyResolver("log.file", null); - LogFile logFile = LogFile.get(resolver); - Properties properties = new Properties(); - logFile.applyTo(properties); - assertThat(logFile.toString()).isEqualTo("log.file"); - assertThat(properties.getProperty(LoggingSystemProperties.LOG_FILE)) - .isEqualTo("log.file"); - assertThat(properties.getProperty(LoggingSystemProperties.LOG_PATH)).isNull(); + PropertyResolver resolver = getPropertyResolver( + Collections.singletonMap("logging.file.name", "log.file")); + testLoggingFile(resolver); } @Test @Deprecated public void loggingFileWithDeprecatedProperties() { - PropertyResolver resolver = getPropertyResolverWithDeprecatedProperties( - "log.file", null); + PropertyResolver resolver = getPropertyResolver( + Collections.singletonMap("logging.file", "log.file")); + testLoggingFile(resolver); + } + + private void testLoggingFile(PropertyResolver resolver) { LogFile logFile = LogFile.get(resolver); Properties properties = new Properties(); logFile.applyTo(properties); @@ -72,22 +72,20 @@ public class LogFileTests { @Test public void loggingPath() { - PropertyResolver resolver = getPropertyResolver(null, "logpath"); - LogFile logFile = LogFile.get(resolver); - Properties properties = new Properties(); - logFile.applyTo(properties); - assertThat(logFile.toString()).isEqualTo("logpath/spring.log"); - assertThat(properties.getProperty(LoggingSystemProperties.LOG_FILE)) - .isEqualTo("logpath/spring.log"); - assertThat(properties.getProperty(LoggingSystemProperties.LOG_PATH)) - .isEqualTo("logpath"); + PropertyResolver resolver = getPropertyResolver( + Collections.singletonMap("logging.file.path", "logpath")); + testLoggingPath(resolver); } @Test @Deprecated public void loggingPathWithDeprecatedProperties() { - PropertyResolver resolver = getPropertyResolverWithDeprecatedProperties(null, - "logpath"); + PropertyResolver resolver = getPropertyResolver( + Collections.singletonMap("logging.path", "logpath")); + testLoggingPath(resolver); + } + + private void testLoggingPath(PropertyResolver resolver) { LogFile logFile = LogFile.get(resolver); Properties properties = new Properties(); logFile.applyTo(properties); @@ -100,22 +98,24 @@ public class LogFileTests { @Test public void loggingFileAndPath() { - PropertyResolver resolver = getPropertyResolver("log.file", "logpath"); - LogFile logFile = LogFile.get(resolver); - Properties properties = new Properties(); - logFile.applyTo(properties); - assertThat(logFile.toString()).isEqualTo("log.file"); - assertThat(properties.getProperty(LoggingSystemProperties.LOG_FILE)) - .isEqualTo("log.file"); - assertThat(properties.getProperty(LoggingSystemProperties.LOG_PATH)) - .isEqualTo("logpath"); + Map properties = new LinkedHashMap<>(); + properties.put("logging.file.name", "log.file"); + properties.put("logging.file.path", "logpath"); + PropertyResolver resolver = getPropertyResolver(properties); + testLoggingFileAndPath(resolver); } @Test @Deprecated public void loggingFileAndPathWithDeprecatedProperties() { - PropertyResolver resolver = getPropertyResolverWithDeprecatedProperties( - "log.file", "logpath"); + Map properties = new LinkedHashMap<>(); + properties.put("logging.file", "log.file"); + properties.put("logging.path", "logpath"); + PropertyResolver resolver = getPropertyResolver(properties); + testLoggingFileAndPath(resolver); + } + + private void testLoggingFileAndPath(PropertyResolver resolver) { LogFile logFile = LogFile.get(resolver); Properties properties = new Properties(); logFile.applyTo(properties); @@ -126,23 +126,7 @@ public class LogFileTests { .isEqualTo("logpath"); } - private PropertyResolver getPropertyResolver(String file, String path) { - Map properties = new LinkedHashMap<>(); - properties.put("logging.file.name", file); - properties.put("logging.file.path", path); - PropertySource propertySource = new MapPropertySource("properties", - properties); - MutablePropertySources propertySources = new MutablePropertySources(); - propertySources.addFirst(propertySource); - return new PropertySourcesPropertyResolver(propertySources); - } - - @Deprecated - private PropertyResolver getPropertyResolverWithDeprecatedProperties(String file, - String path) { - Map properties = new LinkedHashMap<>(); - properties.put("logging.file", file); - properties.put("logging.path", path); + private PropertyResolver getPropertyResolver(Map properties) { PropertySource propertySource = new MapPropertySource("properties", properties); MutablePropertySources propertySources = new MutablePropertySources();