Support multiple names in <springProfile> Logback element
Closes gh-3493 Closes gh-3494
This commit is contained in:
parent
352ff4e899
commit
125de0211b
|
|
@ -21,6 +21,7 @@ import java.util.List;
|
||||||
|
|
||||||
import org.springframework.core.env.Environment;
|
import org.springframework.core.env.Environment;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
import org.xml.sax.Attributes;
|
import org.xml.sax.Attributes;
|
||||||
|
|
||||||
import ch.qos.logback.core.joran.action.Action;
|
import ch.qos.logback.core.joran.action.Action;
|
||||||
|
|
@ -36,6 +37,7 @@ import ch.qos.logback.core.util.OptionHelper;
|
||||||
* logback configuration to only be enabled when a specific profile is active.
|
* logback configuration to only be enabled when a specific profile is active.
|
||||||
*
|
*
|
||||||
* @author Phillip Webb
|
* @author Phillip Webb
|
||||||
|
* @author Eddú Meléndez
|
||||||
*/
|
*/
|
||||||
class SpringProfileAction extends Action implements InPlayListener {
|
class SpringProfileAction extends Action implements InPlayListener {
|
||||||
|
|
||||||
|
|
@ -65,11 +67,14 @@ class SpringProfileAction extends Action implements InPlayListener {
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean acceptsProfiles(InterpretationContext ic, Attributes attributes) {
|
private boolean acceptsProfiles(InterpretationContext ic, Attributes attributes) {
|
||||||
String profileName = attributes.getValue(NAME_ATTRIBUTE);
|
String[] profileNames = StringUtils.commaDelimitedListToStringArray(attributes
|
||||||
if (!OptionHelper.isEmpty(profileName)) {
|
.getValue(NAME_ATTRIBUTE));
|
||||||
|
if (profileNames.length != 0) {
|
||||||
|
for (String profileName : profileNames) {
|
||||||
OptionHelper.substVars(profileName, ic, this.context);
|
OptionHelper.substVars(profileName, ic, this.context);
|
||||||
|
}
|
||||||
return this.environment != null
|
return this.environment != null
|
||||||
&& this.environment.acceptsProfiles(profileName);
|
&& this.environment.acceptsProfiles(profileNames);
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -40,6 +40,7 @@ import static org.junit.Assert.assertThat;
|
||||||
* Tests for {@link SpringBootJoranConfigurator}.
|
* Tests for {@link SpringBootJoranConfigurator}.
|
||||||
*
|
*
|
||||||
* @author Phillip Webb
|
* @author Phillip Webb
|
||||||
|
* @author Eddú Meléndez
|
||||||
*/
|
*/
|
||||||
public class SpringBootJoranConfiguratorTests {
|
public class SpringBootJoranConfiguratorTests {
|
||||||
|
|
||||||
|
|
@ -79,6 +80,22 @@ public class SpringBootJoranConfiguratorTests {
|
||||||
this.out.expect(containsString("Hello"));
|
this.out.expect(containsString("Hello"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void multipleNamesFirstProfileActive() throws Exception {
|
||||||
|
this.environment.setActiveProfiles("production");
|
||||||
|
initialize("multi-profile-names.xml");
|
||||||
|
this.logger.trace("Hello");
|
||||||
|
this.out.expect(containsString("Hello"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void multipleNamesSecondProfileActive() throws Exception {
|
||||||
|
this.environment.setActiveProfiles("test");
|
||||||
|
initialize("multi-profile-names.xml");
|
||||||
|
this.logger.trace("Hello");
|
||||||
|
this.out.expect(containsString("Hello"));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void profileNotActive() throws Exception {
|
public void profileNotActive() throws Exception {
|
||||||
initialize("production-profile.xml");
|
initialize("production-profile.xml");
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,7 @@
|
||||||
|
<?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>
|
||||||
Loading…
Reference in New Issue