-
Notifications
You must be signed in to change notification settings - Fork 40.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
5 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
...work/boot/logging/logback/SpringProfileIfNestedWithinSecondPhaseElementSanityChecker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Copyright 2012-2022 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.boot.logging.logback; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import ch.qos.logback.classic.joran.sanity.IfNestedWithinSecondPhaseElementSC; | ||
import ch.qos.logback.classic.model.LoggerModel; | ||
import ch.qos.logback.classic.model.RootLoggerModel; | ||
import ch.qos.logback.core.joran.sanity.Pair; | ||
import ch.qos.logback.core.joran.sanity.SanityChecker; | ||
import ch.qos.logback.core.model.AppenderModel; | ||
import ch.qos.logback.core.model.Model; | ||
import ch.qos.logback.core.spi.ContextAwareBase; | ||
|
||
/** | ||
* {@link SanityChecker} to ensure that {@code springProfile} elements are not nested | ||
* within second-phase elements. | ||
* | ||
* @author Phillip Webb | ||
* @see IfNestedWithinSecondPhaseElementSC | ||
*/ | ||
class SpringProfileIfNestedWithinSecondPhaseElementSanityChecker extends ContextAwareBase implements SanityChecker { | ||
|
||
private static final List<Class<? extends Model>> SECOND_PHASE_TYPES = List.of(AppenderModel.class, | ||
LoggerModel.class, RootLoggerModel.class); | ||
|
||
@Override | ||
public void check(Model model) { | ||
if (model == null) { | ||
return; | ||
} | ||
List<Model> models = new ArrayList<>(); | ||
SECOND_PHASE_TYPES.forEach((type) -> deepFindAllModelsOfType(type, models, model)); | ||
List<Pair<Model, Model>> nestedPairs = deepFindNestedSubModelsOfType(SpringProfileModel.class, models); | ||
if (!nestedPairs.isEmpty()) { | ||
addWarn("<springProfile> elements cannot be nested within an <appender>, <logger> or <root> element"); | ||
nestedPairs.forEach((nested) -> { | ||
Model first = nested.first; | ||
Model second = nested.second; | ||
addWarn("Element <%s> at line %s contains a nested <%s> element at line %s".formatted(first.getTag(), | ||
first.getLineNumber(), second.getTag(), second.getLineNumber())); | ||
}); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
spring-boot-project/spring-boot/src/test/resources/logback-springprofile-in-root.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<configuration> | ||
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> | ||
<encoder> | ||
<pattern>%property{LOG_FILE} [%t] ${PID:-????} %c{1}: %m%n BOOTBOOT</pattern> | ||
</encoder> | ||
</appender> | ||
<root level="INFO"> | ||
<springProfile name="profile"> | ||
<appender-ref ref="CONSOLE" /> | ||
</springProfile> | ||
</root> | ||
</configuration> |