forked from spring-projects/spring-boot
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Polish 'Support profile specific Log4j2 configuration'
- Loading branch information
Showing
7 changed files
with
258 additions
and
80 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
162 changes: 162 additions & 0 deletions
162
...boot/src/test/java/org/springframework/boot/logging/log4j2/SpringProfileArbiterTests.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,162 @@ | ||
/* | ||
* 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.log4j2; | ||
|
||
import java.util.Set; | ||
|
||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.apache.logging.log4j.core.LoggerContext; | ||
import org.apache.logging.log4j.core.config.Configuration; | ||
import org.apache.logging.log4j.core.config.Reconfigurable; | ||
import org.apache.logging.log4j.util.PropertiesUtil; | ||
import org.apache.logging.log4j.util.PropertySource; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
|
||
import org.springframework.boot.logging.LoggingInitializationContext; | ||
import org.springframework.boot.testsupport.classpath.ClassPathExclusions; | ||
import org.springframework.boot.testsupport.logging.ConfigureClasspathToPreferLog4j2; | ||
import org.springframework.boot.testsupport.system.CapturedOutput; | ||
import org.springframework.boot.testsupport.system.OutputCaptureExtension; | ||
import org.springframework.mock.env.MockEnvironment; | ||
import org.springframework.test.util.ReflectionTestUtils; | ||
import org.springframework.util.ClassUtils; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* Tests for {@link SpringProfileArbiter}. | ||
* | ||
* @author Phillip Webb | ||
*/ | ||
@ExtendWith(OutputCaptureExtension.class) | ||
@ClassPathExclusions("logback-*.jar") | ||
@ConfigureClasspathToPreferLog4j2 | ||
class SpringProfileArbiterTests { | ||
|
||
private CapturedOutput output; | ||
|
||
private final TestLog4J2LoggingSystem loggingSystem = new TestLog4J2LoggingSystem(); | ||
|
||
private final MockEnvironment environment = new MockEnvironment(); | ||
|
||
private final LoggingInitializationContext initializationContext = new LoggingInitializationContext( | ||
this.environment); | ||
|
||
private Logger logger; | ||
|
||
private Configuration configuration; | ||
|
||
@BeforeEach | ||
void setup(CapturedOutput output) { | ||
this.output = output; | ||
LoggerContext loggerContext = (LoggerContext) LogManager.getContext(false); | ||
this.configuration = loggerContext.getConfiguration(); | ||
this.loggingSystem.cleanUp(); | ||
this.logger = LogManager.getLogger(getClass()); | ||
cleanUpPropertySources(); | ||
} | ||
|
||
@AfterEach | ||
void cleanUp() { | ||
this.loggingSystem.cleanUp(); | ||
LoggerContext loggerContext = (LoggerContext) LogManager.getContext(false); | ||
loggerContext.stop(); | ||
loggerContext.start(((Reconfigurable) this.configuration).reconfigure()); | ||
cleanUpPropertySources(); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
private void cleanUpPropertySources() { // https://issues.apache.org/jira/browse/LOG4J2-3618 | ||
PropertiesUtil properties = PropertiesUtil.getProperties(); | ||
Object environment = ReflectionTestUtils.getField(properties, "environment"); | ||
Set<PropertySource> sources = (Set<PropertySource>) ReflectionTestUtils.getField(environment, "sources"); | ||
sources.removeIf((candidate) -> candidate instanceof SpringEnvironmentPropertySource | ||
|| candidate instanceof SpringBootPropertySource); | ||
} | ||
|
||
@Test | ||
void profileActive() { | ||
this.environment.setActiveProfiles("production"); | ||
initialize("production-profile.xml"); | ||
this.logger.trace("Hello"); | ||
assertThat(this.output).contains("Hello"); | ||
} | ||
|
||
@Test | ||
void multipleNamesFirstProfileActive() { | ||
this.environment.setActiveProfiles("production"); | ||
initialize("multi-profile-names.xml"); | ||
this.logger.trace("Hello"); | ||
assertThat(this.output).contains("Hello"); | ||
} | ||
|
||
@Test | ||
void multipleNamesSecondProfileActive() { | ||
this.environment.setActiveProfiles("test"); | ||
initialize("multi-profile-names.xml"); | ||
this.logger.trace("Hello"); | ||
assertThat(this.output).contains("Hello"); | ||
} | ||
|
||
@Test | ||
void profileNotActive() { | ||
initialize("production-profile.xml"); | ||
this.logger.trace("Hello"); | ||
assertThat(this.output).doesNotContain("Hello"); | ||
} | ||
|
||
@Test | ||
void profileExpressionMatchFirst() { | ||
this.environment.setActiveProfiles("production"); | ||
initialize("profile-expression.xml"); | ||
this.logger.trace("Hello"); | ||
assertThat(this.output).contains("Hello"); | ||
} | ||
|
||
@Test | ||
void profileExpressionMatchSecond() { | ||
this.environment.setActiveProfiles("test"); | ||
initialize("profile-expression.xml"); | ||
this.logger.trace("Hello"); | ||
assertThat(this.output).contains("Hello"); | ||
} | ||
|
||
@Test | ||
void profileExpressionNoMatch() { | ||
this.environment.setActiveProfiles("development"); | ||
initialize("profile-expression.xml"); | ||
this.logger.trace("Hello"); | ||
assertThat(this.output).doesNotContain("Hello"); | ||
} | ||
|
||
private void initialize(String config) { | ||
this.environment.setProperty("logging.log4j2.config.override", getPackageResource(config)); | ||
this.loggingSystem.initialize(this.initializationContext, null, null); | ||
} | ||
|
||
private String getPackageResource(String fileName) { | ||
String path = ClassUtils.getPackageName(getClass()); | ||
path = path.replace('.', '/'); | ||
path = path + "/" + fileName; | ||
return "src/test/resources/" + path; | ||
} | ||
|
||
} |
47 changes: 47 additions & 0 deletions
47
...g-boot/src/test/java/org/springframework/boot/logging/log4j2/TestLog4J2LoggingSystem.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,47 @@ | ||
/* | ||
* 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.log4j2; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.core.config.Configuration; | ||
|
||
class TestLog4J2LoggingSystem extends Log4J2LoggingSystem { | ||
|
||
private List<String> availableClasses = new ArrayList<>(); | ||
|
||
TestLog4J2LoggingSystem() { | ||
super(TestLog4J2LoggingSystem.class.getClassLoader()); | ||
} | ||
|
||
Configuration getConfiguration() { | ||
return ((org.apache.logging.log4j.core.LoggerContext) LogManager.getContext(false)).getConfiguration(); | ||
} | ||
|
||
@Override | ||
protected boolean isClassAvailable(String className) { | ||
return this.availableClasses.contains(className); | ||
} | ||
|
||
void availableClasses(String... classNames) { | ||
Collections.addAll(this.availableClasses, classNames); | ||
} | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
...g-boot/src/test/resources/org/springframework/boot/logging/log4j2/multi-profile-names.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,8 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<Configuration> | ||
<SpringProfile name="production, test"> | ||
<Loggers> | ||
<Logger name="org.springframework.boot.logging.log4j2" level="TRACE" /> | ||
</Loggers> | ||
</SpringProfile> | ||
</Configuration> |
8 changes: 8 additions & 0 deletions
8
...ng-boot/src/test/resources/org/springframework/boot/logging/log4j2/production-profile.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,8 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<Configuration> | ||
<SpringProfile name="production"> | ||
<Loggers> | ||
<Logger name="org.springframework.boot.logging.log4j2" level="TRACE" /> | ||
</Loggers> | ||
</SpringProfile> | ||
</Configuration> |
Oops, something went wrong.