Skip to content

Commit 2e7ca6f

Browse files
committed
Warning if <springProfile> is used in phase 2 model elements
Add `SpringProfileIfNestedWithinSecondPhaseElementSanityChecker` which will provide a warning if `<springProfile>` is used within a phase 2 model element. This is similar to Logback's own `<if>` warnings. The `LogbackLoggingSystem` has also been updated so that warning are printed when present. Fixes gh-33610
1 parent 2ed512d commit 2e7ca6f

File tree

5 files changed

+96
-0
lines changed

5 files changed

+96
-0
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/LogbackLoggingSystem.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@
3636
import ch.qos.logback.core.spi.FilterReply;
3737
import ch.qos.logback.core.status.OnConsoleStatusListener;
3838
import ch.qos.logback.core.status.Status;
39+
import ch.qos.logback.core.status.StatusUtil;
3940
import ch.qos.logback.core.util.StatusListenerConfigHelper;
41+
import ch.qos.logback.core.util.StatusPrinter;
4042
import org.slf4j.ILoggerFactory;
4143
import org.slf4j.Logger;
4244
import org.slf4j.LoggerFactory;
@@ -250,6 +252,9 @@ protected void loadConfiguration(LoggingInitializationContext initializationCont
250252
if (errors.length() > 0) {
251253
throw new IllegalStateException(String.format("Logback configuration error detected: %n%s", errors));
252254
}
255+
if (!StatusUtil.contextHasStatusListener(loggerContext)) {
256+
StatusPrinter.printInCaseOfErrorsOrWarnings(loggerContext);
257+
}
253258
}
254259

255260
private void configureByResourceUrl(LoggingInitializationContext initializationContext, LoggerContext loggerContext,

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/SpringBootJoranConfigurator.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,12 @@ class SpringBootJoranConfigurator extends JoranConfigurator {
8282
this.initializationContext = initializationContext;
8383
}
8484

85+
@Override
86+
protected void sanityCheck(Model topModel) {
87+
super.sanityCheck(topModel);
88+
performCheck(new SpringProfileIfNestedWithinSecondPhaseElementSanityChecker(), topModel);
89+
}
90+
8591
@Override
8692
protected void addModelHandlerAssociations(DefaultProcessor defaultProcessor) {
8793
defaultProcessor.addHandler(SpringPropertyModel.class,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright 2012-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.logging.logback;
18+
19+
import java.util.ArrayList;
20+
import java.util.List;
21+
22+
import ch.qos.logback.classic.joran.sanity.IfNestedWithinSecondPhaseElementSC;
23+
import ch.qos.logback.classic.model.LoggerModel;
24+
import ch.qos.logback.classic.model.RootLoggerModel;
25+
import ch.qos.logback.core.joran.sanity.Pair;
26+
import ch.qos.logback.core.joran.sanity.SanityChecker;
27+
import ch.qos.logback.core.model.AppenderModel;
28+
import ch.qos.logback.core.model.Model;
29+
import ch.qos.logback.core.spi.ContextAwareBase;
30+
31+
/**
32+
* {@link SanityChecker} to ensure that {@code springProfile} elements are not nested
33+
* within second-phase elements.
34+
*
35+
* @author Phillip Webb
36+
* @see IfNestedWithinSecondPhaseElementSC
37+
*/
38+
class SpringProfileIfNestedWithinSecondPhaseElementSanityChecker extends ContextAwareBase implements SanityChecker {
39+
40+
private static final List<Class<? extends Model>> SECOND_PHASE_TYPES = List.of(AppenderModel.class,
41+
LoggerModel.class, RootLoggerModel.class);
42+
43+
@Override
44+
public void check(Model model) {
45+
if (model == null) {
46+
return;
47+
}
48+
List<Model> models = new ArrayList<>();
49+
SECOND_PHASE_TYPES.forEach((type) -> deepFindAllModelsOfType(type, models, model));
50+
List<Pair<Model, Model>> nestedPairs = deepFindNestedSubModelsOfType(SpringProfileModel.class, models);
51+
if (!nestedPairs.isEmpty()) {
52+
addWarn("<springProfile> elements cannot be nested within an <appender>, <logger> or <root> element");
53+
nestedPairs.forEach((nested) -> {
54+
Model first = nested.first;
55+
Model second = nested.second;
56+
addWarn("Element <%s> at line %s contains a nested <%s> element at line %s".formatted(first.getTag(),
57+
first.getLineNumber(), second.getTag(), second.getLineNumber()));
58+
});
59+
}
60+
}
61+
62+
}

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/logback/LogbackLoggingSystemTests.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import ch.qos.logback.core.encoder.LayoutWrappingEncoder;
3939
import ch.qos.logback.core.rolling.RollingFileAppender;
4040
import ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy;
41+
import ch.qos.logback.core.util.StatusPrinter;
4142
import org.junit.jupiter.api.AfterEach;
4243
import org.junit.jupiter.api.BeforeEach;
4344
import org.junit.jupiter.api.Test;
@@ -110,6 +111,7 @@ void setup() {
110111
ConversionService conversionService = ApplicationConversionService.getSharedInstance();
111112
this.environment.setConversionService((ConfigurableConversionService) conversionService);
112113
this.initializationContext = new LoggingInitializationContext(this.environment);
114+
StatusPrinter.setPrintStream(System.out);
113115
}
114116

115117
@AfterEach
@@ -665,6 +667,14 @@ void whenContextHasAotContributionThenProcessAheadOfTimeClearsAndReturnsIt() {
665667
assertThat(contribution).isNotNull();
666668
}
667669

670+
@Test // gh-33610
671+
void springProfileIfNestedWithinSecondPhaseElementSanityChecker(CapturedOutput output) {
672+
this.loggingSystem.beforeInitialize();
673+
initialize(this.initializationContext, "classpath:logback-springprofile-in-root.xml", null);
674+
this.logger.info("Hello world");
675+
assertThat(output).contains("<springProfile> elements cannot be nested within an");
676+
}
677+
668678
private void initialize(LoggingInitializationContext context, String configLocation, LogFile logFile) {
669679
this.loggingSystem.getSystemProperties((ConfigurableEnvironment) context.getEnvironment()).apply(logFile);
670680
this.loggingSystem.initialize(context, configLocation, logFile);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<configuration>
3+
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
4+
<encoder>
5+
<pattern>%property{LOG_FILE} [%t] ${PID:-????} %c{1}: %m%n BOOTBOOT</pattern>
6+
</encoder>
7+
</appender>
8+
<root level="INFO">
9+
<springProfile name="profile">
10+
<appender-ref ref="CONSOLE" />
11+
</springProfile>
12+
</root>
13+
</configuration>

0 commit comments

Comments
 (0)