parent
c86284cea3
commit
a66045fa98
|
|
@ -1351,11 +1351,14 @@ for use within Logback. This can be useful if you want to access values from you
|
|||
`application.properties` file in your logback configuration. The tag works in a similar
|
||||
way to Logback's standard `<property>` tag, but rather than specifying a direct `value`
|
||||
you specify the `source` of the property (from the `Environment`). You can use the `scope`
|
||||
attribute if you need to store the property somewhere other than in `local` scope.
|
||||
attribute if you need to store the property somewhere other than in `local` scope. If
|
||||
you need a fallback value in case the property is not set in the `Environment`, you can
|
||||
use the `defaultValue` attribute.
|
||||
|
||||
[source,xml,indent=0]
|
||||
----
|
||||
<springProperty scope="context" name="fluentHost" source="myapp.fluentd.host"/>
|
||||
<springProperty scope="context" name="fluentHost" source="myapp.fluentd.host"
|
||||
defaultValue="localhost"/>
|
||||
<appender name="FLUENT" class="ch.qos.logback.more.appenders.DataFluentAppender">
|
||||
<remoteHost>${fluentHost}</remoteHost>
|
||||
...
|
||||
|
|
|
|||
|
|
@ -62,9 +62,6 @@ class SpringPropertyAction extends Action {
|
|||
private String getValue(String source, String defaultValue) {
|
||||
if (this.environment == null) {
|
||||
addWarn("No Spring Environment available to resolve " + source);
|
||||
return null;
|
||||
}
|
||||
if (source == null) {
|
||||
return defaultValue;
|
||||
}
|
||||
String value = this.environment.getProperty(source);
|
||||
|
|
@ -76,9 +73,9 @@ class SpringPropertyAction extends Action {
|
|||
String prefix = source.substring(0, lastDot + 1);
|
||||
RelaxedPropertyResolver resolver = new RelaxedPropertyResolver(
|
||||
this.environment, prefix);
|
||||
return resolver.getProperty(source.substring(lastDot + 1));
|
||||
return resolver.getProperty(source.substring(lastDot + 1), defaultValue);
|
||||
}
|
||||
return null;
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -41,6 +41,7 @@ import static org.hamcrest.Matchers.not;
|
|||
*
|
||||
* @author Phillip Webb
|
||||
* @author Eddú Meléndez
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
public class SpringBootJoranConfiguratorTests {
|
||||
|
||||
|
|
@ -140,10 +141,28 @@ public class SpringBootJoranConfiguratorTests {
|
|||
assertThat(this.context.getProperty("MINE")).isEqualTo("test");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void springPropertyNoValue() throws Exception {
|
||||
initialize("property.xml");
|
||||
assertThat(this.context.getProperty("SIMPLE")).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void relaxedSpringPropertyNoValue() throws Exception {
|
||||
initialize("property.xml");
|
||||
assertThat(this.context.getProperty("MINE")).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void springPropertyWithDefaultValue() throws Exception {
|
||||
initialize("property-defaultValue.xml");
|
||||
assertThat(this.context.getProperty("MINE")).isEqualTo("foo");
|
||||
initialize("property-default-value.xml");
|
||||
assertThat(this.context.getProperty("SIMPLE")).isEqualTo("foo");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void relaxedSpringPropertyWithDefaultValue() throws Exception {
|
||||
initialize("property-default-value.xml");
|
||||
assertThat(this.context.getProperty("MINE")).isEqualTo("bar");
|
||||
}
|
||||
|
||||
private void doTestNestedProfile(boolean expected, String... profiles)
|
||||
|
|
|
|||
|
|
@ -0,0 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<include resource="org/springframework/boot/logging/logback/base.xml" />
|
||||
<springProperty scope="context" name="SIMPLE" source="testpropertyfoobar" defaultValue="foo"/>
|
||||
<springProperty scope="context" name="MINE" source="my.example-property" defaultValue="bar"/>
|
||||
</configuration>
|
||||
|
|
@ -1,5 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<include resource="org/springframework/boot/logging/logback/base.xml" />
|
||||
<springProperty scope="context" name="MINE" defaultValue="foo"/>
|
||||
</configuration>
|
||||
|
|
@ -1,5 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<include resource="org/springframework/boot/logging/logback/base.xml" />
|
||||
<springProperty scope="context" name="SIMPLE" source="testpropertyfoobar"/>
|
||||
<springProperty scope="context" name="MINE" source="my.example-property"/>
|
||||
</configuration>
|
||||
|
|
|
|||
Loading…
Reference in New Issue