Commit 7939b8b4 authored by Stephane Nicoll's avatar Stephane Nicoll

Polish "Rename logging.file to logging.file.name"

Closes gh-15089
parent 21da4a5d
...@@ -58,27 +58,20 @@ public class LogFileWebEndpointAutoConfiguration { ...@@ -58,27 +58,20 @@ public class LogFileWebEndpointAutoConfiguration {
private static class LogFileCondition extends SpringBootCondition { private static class LogFileCondition extends SpringBootCondition {
@SuppressWarnings("deprecation")
@Override @Override
public ConditionOutcome getMatchOutcome(ConditionContext context, public ConditionOutcome getMatchOutcome(ConditionContext context,
AnnotatedTypeMetadata metadata) { AnnotatedTypeMetadata metadata) {
Environment environment = context.getEnvironment(); Environment environment = context.getEnvironment();
String config = environment String config = getLogFileConfig(environment, LogFile.FILE_NAME_PROPERTY,
.resolvePlaceholders("${" + LogFile.FILE_NAME_PROPERTY + ":}"); LogFile.FILE_PROPERTY);
if (!StringUtils.hasText(config)) {
config = environment
.resolvePlaceholders("${" + LogFile.FILE_PROPERTY + ":}");
}
ConditionMessage.Builder message = ConditionMessage.forCondition("Log File"); ConditionMessage.Builder message = ConditionMessage.forCondition("Log File");
if (StringUtils.hasText(config)) { if (StringUtils.hasText(config)) {
return ConditionOutcome return ConditionOutcome
.match(message.found(LogFile.FILE_NAME_PROPERTY).items(config)); .match(message.found(LogFile.FILE_NAME_PROPERTY).items(config));
} }
config = environment config = getLogFileConfig(environment, LogFile.FILE_PATH_PROPERTY,
.resolvePlaceholders("${" + LogFile.FILE_PATH_PROPERTY + ":}"); LogFile.PATH_PROPERTY);
if (!StringUtils.hasText(config)) {
config = environment
.resolvePlaceholders("${" + LogFile.PATH_PROPERTY + ":}");
}
if (StringUtils.hasText(config)) { if (StringUtils.hasText(config)) {
return ConditionOutcome return ConditionOutcome
.match(message.found(LogFile.FILE_PATH_PROPERTY).items(config)); .match(message.found(LogFile.FILE_PATH_PROPERTY).items(config));
...@@ -92,6 +85,15 @@ public class LogFileWebEndpointAutoConfiguration { ...@@ -92,6 +85,15 @@ public class LogFileWebEndpointAutoConfiguration {
return ConditionOutcome.noMatch(message.didNotFind("logging file").atAll()); 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 + ":}");
}
} }
} }
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
......
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
......
...@@ -37,12 +37,12 @@ content into your application. Rather, pick only the properties that you need. ...@@ -37,12 +37,12 @@ content into your application. Rather, pick only the properties that you need.
# LOGGING # LOGGING
logging.config= # Location of the logging configuration file. For instance, `classpath:logback.xml` for Logback. 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.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-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.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.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.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.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.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. logging.pattern.file= # Appender pattern for output to a file. Supported only with the default Logback setup.
......
...@@ -164,9 +164,9 @@ use the following additional endpoints: ...@@ -164,9 +164,9 @@ use the following additional endpoints:
|Yes |Yes
|`logfile` |`logfile`
|Returns the contents of the logfile (if `logging.file.name` or `logging.file.path` properties have |Returns the contents of the logfile (if `logging.file.name` or `logging.file.path`
been set). Supports the use of the HTTP `Range` header to retrieve part of the log file's properties have been set). Supports the use of the HTTP `Range` header to retrieve part of
content. the log file's content.
|Yes |Yes
|`prometheus` |`prometheus`
......
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -131,19 +131,25 @@ public class LogFile { ...@@ -131,19 +131,25 @@ public class LogFile {
* @return a {@link LogFile} or {@code null} if the environment didn't contain any * @return a {@link LogFile} or {@code null} if the environment didn't contain any
* suitable properties * suitable properties
*/ */
@SuppressWarnings("deprecation")
public static LogFile get(PropertyResolver propertyResolver) { public static LogFile get(PropertyResolver propertyResolver) {
String file = propertyResolver.getProperty(FILE_NAME_PROPERTY); String file = getLogFileProperty(propertyResolver, FILE_NAME_PROPERTY,
String path = propertyResolver.getProperty(FILE_PATH_PROPERTY); FILE_PROPERTY);
if (file == null) { String path = getLogFileProperty(propertyResolver, FILE_PATH_PROPERTY,
file = propertyResolver.getProperty(FILE_PROPERTY); PATH_PROPERTY);
}
if (path == null) {
path = propertyResolver.getProperty(PATH_PROPERTY);
}
if (StringUtils.hasLength(file) || StringUtils.hasLength(path)) { if (StringUtils.hasLength(file) || StringUtils.hasLength(path)) {
return new LogFile(file, path); return new LogFile(file, path);
} }
return null; 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);
}
} }
...@@ -67,6 +67,15 @@ ...@@ -67,6 +67,15 @@
"defaultValue": "%wEx", "defaultValue": "%wEx",
"sourceType": "org.springframework.boot.context.logging.LoggingApplicationListener" "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", "name": "logging.file.name",
"type": "java.lang.String", "type": "java.lang.String",
...@@ -105,6 +114,15 @@ ...@@ -105,6 +114,15 @@
"description": "Log levels severity mapping. For instance, `logging.level.org.springframework=DEBUG`.", "description": "Log levels severity mapping. For instance, `logging.level.org.springframework=DEBUG`.",
"sourceType": "org.springframework.boot.context.logging.LoggingApplicationListener" "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", "name": "logging.pattern.console",
"type": "java.lang.String", "type": "java.lang.String",
...@@ -332,28 +350,8 @@ ...@@ -332,28 +350,8 @@
"type": "java.lang.Integer", "type": "java.lang.Integer",
"description": "Application index.", "description": "Application index.",
"deprecation": { "deprecation": {
"reason": "Application context ids are now unique by default.", "level": "error",
"level": "error" "reason": "Application context ids are now unique by default."
}
},
{
"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"
} }
} }
], ],
...@@ -366,6 +364,7 @@ ...@@ -366,6 +364,7 @@
"parameters": { "parameters": {
"group": false "group": false
} }
} }
] ]
}, },
......
...@@ -224,6 +224,22 @@ public class LoggingApplicationListenerTests { ...@@ -224,6 +224,22 @@ public class LoggingApplicationListenerTests {
assertThat(output).startsWith("target/foo.log"); 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 @Test
public void addLogFilePropertyWithDefault() { public void addLogFilePropertyWithDefault() {
assertThat(new File("target/foo.log").exists()).isFalse(); assertThat(new File("target/foo.log").exists()).isFalse();
...@@ -236,6 +252,19 @@ public class LoggingApplicationListenerTests { ...@@ -236,6 +252,19 @@ public class LoggingApplicationListenerTests {
assertThat(new File("target/foo.log").exists()).isTrue(); 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 @Test
public void addLogPathProperty() { public void addLogPathProperty() {
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context, TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
...@@ -251,6 +280,21 @@ public class LoggingApplicationListenerTests { ...@@ -251,6 +280,21 @@ public class LoggingApplicationListenerTests {
assertThat(output).startsWith("target/foo/spring.log"); 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 @Test
public void parseDebugArg() { public void parseDebugArg() {
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context, "debug"); TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context, "debug");
...@@ -516,24 +560,13 @@ public class LoggingApplicationListenerTests { ...@@ -516,24 +560,13 @@ public class LoggingApplicationListenerTests {
@Deprecated @Deprecated
public void systemPropertiesAreSetForLoggingConfigurationWithDeprecatedProperties() { public void systemPropertiesAreSetForLoggingConfigurationWithDeprecatedProperties() {
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context, TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"logging.exception-conversion-word=conversion", "logging.file=target/log", "logging.file=target/log", "logging.path=path");
"logging.path=path", "logging.pattern.console=console",
"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(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)) assertThat(System.getProperty(LoggingSystemProperties.LOG_FILE))
.isEqualTo("target/log"); .isEqualTo("target/log");
assertThat(System.getProperty(LoggingSystemProperties.LOG_LEVEL_PATTERN))
.isEqualTo("level");
assertThat(System.getProperty(LoggingSystemProperties.LOG_PATH)) assertThat(System.getProperty(LoggingSystemProperties.LOG_PATH))
.isEqualTo("path"); .isEqualTo("path");
assertThat(System.getProperty(LoggingSystemProperties.PID_KEY)).isNotNull();
} }
@Test @Test
......
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
package org.springframework.boot.logging; package org.springframework.boot.logging;
import java.util.Collections;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
import java.util.Properties; import java.util.Properties;
...@@ -39,28 +40,27 @@ public class LogFileTests { ...@@ -39,28 +40,27 @@ public class LogFileTests {
@Test @Test
public void noProperties() { public void noProperties() {
PropertyResolver resolver = getPropertyResolver(null, null); PropertyResolver resolver = getPropertyResolver(Collections.emptyMap());
LogFile logFile = LogFile.get(resolver); LogFile logFile = LogFile.get(resolver);
assertThat(logFile).isNull(); assertThat(logFile).isNull();
} }
@Test @Test
public void loggingFile() { public void loggingFile() {
PropertyResolver resolver = getPropertyResolver("log.file", null); PropertyResolver resolver = getPropertyResolver(
LogFile logFile = LogFile.get(resolver); Collections.singletonMap("logging.file.name", "log.file"));
Properties properties = new Properties(); testLoggingFile(resolver);
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 @Test
@Deprecated @Deprecated
public void loggingFileWithDeprecatedProperties() { public void loggingFileWithDeprecatedProperties() {
PropertyResolver resolver = getPropertyResolverWithDeprecatedProperties( PropertyResolver resolver = getPropertyResolver(
"log.file", null); Collections.singletonMap("logging.file", "log.file"));
testLoggingFile(resolver);
}
private void testLoggingFile(PropertyResolver resolver) {
LogFile logFile = LogFile.get(resolver); LogFile logFile = LogFile.get(resolver);
Properties properties = new Properties(); Properties properties = new Properties();
logFile.applyTo(properties); logFile.applyTo(properties);
...@@ -72,22 +72,20 @@ public class LogFileTests { ...@@ -72,22 +72,20 @@ public class LogFileTests {
@Test @Test
public void loggingPath() { public void loggingPath() {
PropertyResolver resolver = getPropertyResolver(null, "logpath"); PropertyResolver resolver = getPropertyResolver(
LogFile logFile = LogFile.get(resolver); Collections.singletonMap("logging.file.path", "logpath"));
Properties properties = new Properties(); testLoggingPath(resolver);
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 @Test
@Deprecated @Deprecated
public void loggingPathWithDeprecatedProperties() { public void loggingPathWithDeprecatedProperties() {
PropertyResolver resolver = getPropertyResolverWithDeprecatedProperties(null, PropertyResolver resolver = getPropertyResolver(
"logpath"); Collections.singletonMap("logging.path", "logpath"));
testLoggingPath(resolver);
}
private void testLoggingPath(PropertyResolver resolver) {
LogFile logFile = LogFile.get(resolver); LogFile logFile = LogFile.get(resolver);
Properties properties = new Properties(); Properties properties = new Properties();
logFile.applyTo(properties); logFile.applyTo(properties);
...@@ -100,22 +98,24 @@ public class LogFileTests { ...@@ -100,22 +98,24 @@ public class LogFileTests {
@Test @Test
public void loggingFileAndPath() { public void loggingFileAndPath() {
PropertyResolver resolver = getPropertyResolver("log.file", "logpath"); Map<String, Object> properties = new LinkedHashMap<>();
LogFile logFile = LogFile.get(resolver); properties.put("logging.file.name", "log.file");
Properties properties = new Properties(); properties.put("logging.file.path", "logpath");
logFile.applyTo(properties); PropertyResolver resolver = getPropertyResolver(properties);
assertThat(logFile.toString()).isEqualTo("log.file"); testLoggingFileAndPath(resolver);
assertThat(properties.getProperty(LoggingSystemProperties.LOG_FILE))
.isEqualTo("log.file");
assertThat(properties.getProperty(LoggingSystemProperties.LOG_PATH))
.isEqualTo("logpath");
} }
@Test @Test
@Deprecated @Deprecated
public void loggingFileAndPathWithDeprecatedProperties() { public void loggingFileAndPathWithDeprecatedProperties() {
PropertyResolver resolver = getPropertyResolverWithDeprecatedProperties( Map<String, Object> properties = new LinkedHashMap<>();
"log.file", "logpath"); 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); LogFile logFile = LogFile.get(resolver);
Properties properties = new Properties(); Properties properties = new Properties();
logFile.applyTo(properties); logFile.applyTo(properties);
...@@ -126,23 +126,7 @@ public class LogFileTests { ...@@ -126,23 +126,7 @@ public class LogFileTests {
.isEqualTo("logpath"); .isEqualTo("logpath");
} }
private PropertyResolver getPropertyResolver(String file, String path) { private PropertyResolver getPropertyResolver(Map<String, Object> properties) {
Map<String, Object> 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<String, Object> properties = new LinkedHashMap<>();
properties.put("logging.file", file);
properties.put("logging.path", path);
PropertySource<?> propertySource = new MapPropertySource("properties", PropertySource<?> propertySource = new MapPropertySource("properties",
properties); properties);
MutablePropertySources propertySources = new MutablePropertySources(); MutablePropertySources propertySources = new MutablePropertySources();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment