Commit b4584e6a authored by Stephane Nicoll's avatar Stephane Nicoll

Support profile expression in Logback's <springProfile>

Closes gh-13496
parent a89b2ae4
...@@ -1820,8 +1820,12 @@ ERROR in ch.qos.logback.core.joran.spi.Interpreter@4:71 - no applicable action f ...@@ -1820,8 +1820,12 @@ ERROR in ch.qos.logback.core.joran.spi.Interpreter@4:71 - no applicable action f
The `<springProfile>` tag lets you optionally include or exclude sections of The `<springProfile>` tag lets you optionally include or exclude sections of
configuration based on the active Spring profiles. Profile sections are supported configuration based on the active Spring profiles. Profile sections are supported
anywhere within the `<configuration>` element. Use the `name` attribute to specify which anywhere within the `<configuration>` element. Use the `name` attribute to specify which
profile accepts the configuration. Multiple profiles can be specified with a profile accepts the configuration. The `<springProfile> tag can contains a simple profile
comma-separated list. The following listing shows three sample profiles: name (for example `staging`) or a profile expression. A profile expression allows for more
complicated profile logic to be expressed, for example
`production & (eu-central | eu-west)`. Check the
{spring-reference}core.html#beans-definition-profiles-java[reference guide] for more
details. The following listing shows three sample profiles:
[source,xml,indent=0] [source,xml,indent=0]
---- ----
...@@ -1829,7 +1833,7 @@ comma-separated list. The following listing shows three sample profiles: ...@@ -1829,7 +1833,7 @@ comma-separated list. The following listing shows three sample profiles:
<!-- configuration to be enabled when the "staging" profile is active --> <!-- configuration to be enabled when the "staging" profile is active -->
</springProfile> </springProfile>
<springProfile name="dev, staging"> <springProfile name="dev | staging">
<!-- configuration to be enabled when the "dev" or "staging" profiles are active --> <!-- configuration to be enabled when the "dev" or "staging" profiles are active -->
</springProfile> </springProfile>
......
/* /*
* 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.
...@@ -29,6 +29,7 @@ import ch.qos.logback.core.util.OptionHelper; ...@@ -29,6 +29,7 @@ import ch.qos.logback.core.util.OptionHelper;
import org.xml.sax.Attributes; import org.xml.sax.Attributes;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
...@@ -74,7 +75,7 @@ class SpringProfileAction extends Action implements InPlayListener { ...@@ -74,7 +75,7 @@ class SpringProfileAction extends Action implements InPlayListener {
OptionHelper.substVars(profileName, ic, this.context); OptionHelper.substVars(profileName, ic, this.context);
} }
return this.environment != null return this.environment != null
&& this.environment.acceptsProfiles(profileNames); && this.environment.acceptsProfiles(Profiles.of(profileNames));
} }
return false; return false;
} }
......
/* /*
* 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.
...@@ -108,6 +108,30 @@ public class SpringBootJoranConfiguratorTests { ...@@ -108,6 +108,30 @@ public class SpringBootJoranConfiguratorTests {
this.out.expect(not(containsString("Hello"))); this.out.expect(not(containsString("Hello")));
} }
@Test
public void profileExpressionMatchFirst() throws Exception {
this.environment.setActiveProfiles("production");
initialize("profile-expression.xml");
this.logger.trace("Hello");
this.out.expect(containsString("Hello"));
}
@Test
public void profileExpressionMatchSecond() throws Exception {
this.environment.setActiveProfiles("production");
initialize("profile-expression.xml");
this.logger.trace("Hello");
this.out.expect(containsString("Hello"));
}
@Test
public void profileExpressionNoMatch() throws Exception {
this.environment.setActiveProfiles("development");
initialize("profile-expression.xml");
this.logger.trace("Hello");
this.out.expect(not(containsString("Hello")));
}
@Test @Test
public void profileNestedActiveActive() throws Exception { public void profileNestedActiveActive() throws Exception {
doTestNestedProfile(true, "outer", "inner"); doTestNestedProfile(true, "outer", "inner");
......
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml" />
<springProfile name="production | test">
<logger name="org.springframework.boot.logging.logback" level="TRACE" />
</springProfile>
</configuration>
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