Support multiple names in <springProfile> Logback element

Closes gh-3493
Closes gh-3494
This commit is contained in:
Eddú Meléndez 2015-07-14 17:26:46 -05:00 committed by Andy Wilkinson
parent 352ff4e899
commit 125de0211b
3 changed files with 33 additions and 4 deletions

View File

@ -21,6 +21,7 @@ import java.util.List;
import org.springframework.core.env.Environment;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.xml.sax.Attributes;
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.
*
* @author Phillip Webb
* @author Eddú Meléndez
*/
class SpringProfileAction extends Action implements InPlayListener {
@ -65,11 +67,14 @@ class SpringProfileAction extends Action implements InPlayListener {
}
private boolean acceptsProfiles(InterpretationContext ic, Attributes attributes) {
String profileName = attributes.getValue(NAME_ATTRIBUTE);
if (!OptionHelper.isEmpty(profileName)) {
String[] profileNames = StringUtils.commaDelimitedListToStringArray(attributes
.getValue(NAME_ATTRIBUTE));
if (profileNames.length != 0) {
for (String profileName : profileNames) {
OptionHelper.substVars(profileName, ic, this.context);
}
return this.environment != null
&& this.environment.acceptsProfiles(profileName);
&& this.environment.acceptsProfiles(profileNames);
}
return false;
}

View File

@ -40,6 +40,7 @@ import static org.junit.Assert.assertThat;
* Tests for {@link SpringBootJoranConfigurator}.
*
* @author Phillip Webb
* @author Eddú Meléndez
*/
public class SpringBootJoranConfiguratorTests {
@ -79,6 +80,22 @@ public class SpringBootJoranConfiguratorTests {
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
public void profileNotActive() throws Exception {
initialize("production-profile.xml");

View File

@ -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>