Allow spring properties to be used in logback <if> blocks

Reorder `SpringBootJoranConfigurator.addModelHandlerAssociations` so
that handlers are added before calling the super method. Prior to this
commit, handlers were added behind filters which prevented them from
being used in `<if>` blocks.

Fixes gh-33028
This commit is contained in:
Phillip Webb 2022-11-07 14:45:43 -08:00
parent 95557ddbc6
commit 0bdf7e8af7
5 changed files with 33 additions and 1 deletions

View File

@ -55,6 +55,13 @@ bom {
]
}
}
library("Janino", "3.1.8") {
group("org.codehaus.janino") {
imports = [
"janino"
]
}
}
library("JLine", "2.11") {
prohibit("[2.12,)") {
because "it contains breaking changes"

View File

@ -115,6 +115,7 @@ dependencies {
testImplementation("org.apache.derby:derby")
testImplementation("org.apache.derby:derbytools")
testImplementation("org.awaitility:awaitility")
testImplementation("org.codehaus.janino:janino")
testImplementation("org.eclipse.jetty:jetty-client")
testImplementation("org.eclipse.jetty.http2:http2-client")
testImplementation("org.eclipse.jetty.http2:http2-http-client-transport")

View File

@ -84,13 +84,13 @@ class SpringBootJoranConfigurator extends JoranConfigurator {
@Override
protected void addModelHandlerAssociations(DefaultProcessor defaultProcessor) {
super.addModelHandlerAssociations(defaultProcessor);
defaultProcessor.addHandler(SpringPropertyModel.class,
(handlerContext, handlerMic) -> new SpringPropertyModelHandler(this.context,
this.initializationContext.getEnvironment()));
defaultProcessor.addHandler(SpringProfileModel.class,
(handlerContext, handlerMic) -> new SpringProfileModelHandler(this.context,
this.initializationContext.getEnvironment()));
super.addModelHandlerAssociations(defaultProcessor);
}
@Override

View File

@ -189,6 +189,20 @@ class SpringBootJoranConfiguratorTests {
assertThat(this.context.getProperty("MINE")).isEqualTo("bar");
}
@Test
void springPropertyInIfWhenTrue() throws Exception {
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment, "my.example-property=true");
initialize("property-in-if.xml");
assertThat(this.context.getProperty("MYCHECK")).isEqualTo("i-was-included");
}
@Test
void springPropertyInIfWhenFalse() throws Exception {
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment, "my.example-property=false");
initialize("property-in-if.xml");
assertThat(this.context.getProperty("MYCHECK")).isNull();
}
@Test
void addsAotContributionToContextDuringAotProcessing() throws Exception {
withSystemProperty("spring.aot.processing", "true", () -> {

View File

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml" />
<springProperty scope="context" name="MINE" source="my.example-property" />
<if condition='property("MINE").contains("true")'>
<then>
<variable scope="context" name="MYCHECK" value="i-was-included" />
</then>
</if>
</configuration>