Merge pull request for #32578 from rgoers
* pr/32578: Document Log4j2 extensions * Merge pull request for #32735: Polish 'Resolve URLs using Log4J2 mechanisms' Resolve URLs using Log4j2 mechanisms * Merge pull request for #32734: Polish 'Support profile specific Log4j2 configuration' Support profile specific Log4j2 configuration * Merge pull request for #32733: Polish 'Add Log4J2 PropertySource backed by the Spring Environment' Add Log4J2 PropertySource backed by the Spring Environment * Merge pull request for #32732: Polish 'Support Log4J2 string lookups from the Spring Environment' Support Log4J2 string lookups from the Spring Environment * Merge pull request for #32730: Polish 'Add Spring Environment to LoggerContext' Add Spring Environment to LoggerContext * Merge pull request for #32730: Polish 'Support 'log4j.configurationFile' system property' Support 'log4j.configurationFile' system property Closes gh-32578pull/32740/head
commit
b12fda2a72
@ -0,0 +1,55 @@
|
||||
/*
|
||||
* 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 org.apache.logging.log4j.core.LogEvent;
|
||||
import org.apache.logging.log4j.core.LoggerContext;
|
||||
import org.apache.logging.log4j.core.config.LoggerContextAware;
|
||||
import org.apache.logging.log4j.core.config.plugins.Plugin;
|
||||
import org.apache.logging.log4j.core.lookup.StrLookup;
|
||||
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Lookup for Spring properties.
|
||||
*
|
||||
* @author Ralph Goers
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
@Plugin(name = "spring", category = StrLookup.CATEGORY)
|
||||
class SpringEnvironmentLookup implements LoggerContextAware, StrLookup {
|
||||
|
||||
private volatile Environment environment;
|
||||
|
||||
@Override
|
||||
public String lookup(LogEvent event, String key) {
|
||||
return lookup(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String lookup(String key) {
|
||||
Assert.state(this.environment != null, "Unable to obtain Spring Environment from LoggerContext");
|
||||
return (this.environment != null) ? this.environment.getProperty(key) : null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setLoggerContext(LoggerContext loggerContext) {
|
||||
this.environment = Log4J2LoggingSystem.getEnvironment(loggerContext);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,58 @@
|
||||
/*
|
||||
* 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 org.apache.logging.log4j.util.PropertySource;
|
||||
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Returns properties from Spring.
|
||||
*
|
||||
* @author Ralph Goers
|
||||
*/
|
||||
class SpringEnvironmentPropertySource implements PropertySource {
|
||||
|
||||
/**
|
||||
* System properties take precedence followed by properties in Log4j properties files.
|
||||
*/
|
||||
private static final int PRIORITY = -100;
|
||||
|
||||
private final Environment environment;
|
||||
|
||||
SpringEnvironmentPropertySource(Environment environment) {
|
||||
Assert.notNull(environment, "Environment must not be null");
|
||||
this.environment = environment;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getPriority() {
|
||||
return PRIORITY;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getProperty(String key) {
|
||||
return this.environment.getProperty(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsProperty(String key) {
|
||||
return this.environment.containsProperty(key);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,108 @@
|
||||
/*
|
||||
* 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 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.Node;
|
||||
import org.apache.logging.log4j.core.config.arbiters.Arbiter;
|
||||
import org.apache.logging.log4j.core.config.plugins.Plugin;
|
||||
import org.apache.logging.log4j.core.config.plugins.PluginBuilderAttribute;
|
||||
import org.apache.logging.log4j.core.config.plugins.PluginBuilderFactory;
|
||||
import org.apache.logging.log4j.core.config.plugins.PluginConfiguration;
|
||||
import org.apache.logging.log4j.core.config.plugins.PluginLoggerContext;
|
||||
import org.apache.logging.log4j.status.StatusLogger;
|
||||
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.core.env.Profiles;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* An Arbiter that uses the active Spring profile to determine if configuration should be
|
||||
* included.
|
||||
*
|
||||
* @author Ralph Goers
|
||||
*/
|
||||
@Plugin(name = "SpringProfile", category = Node.CATEGORY, elementType = Arbiter.ELEMENT_TYPE, deferChildren = true,
|
||||
printObject = true)
|
||||
final class SpringProfileArbiter implements Arbiter {
|
||||
|
||||
private final Environment environment;
|
||||
|
||||
private final Profiles profiles;
|
||||
|
||||
private SpringProfileArbiter(Environment environment, String[] profiles) {
|
||||
this.environment = environment;
|
||||
this.profiles = Profiles.of(profiles);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isCondition() {
|
||||
return (this.environment != null) ? this.environment.acceptsProfiles(this.profiles) : false;
|
||||
}
|
||||
|
||||
@PluginBuilderFactory
|
||||
static Builder newBuilder() {
|
||||
return new Builder();
|
||||
}
|
||||
|
||||
/**
|
||||
* Standard Builder to create the Arbiter.
|
||||
*/
|
||||
public static final class Builder implements org.apache.logging.log4j.core.util.Builder<SpringProfileArbiter> {
|
||||
|
||||
private static final Logger statusLogger = StatusLogger.getLogger();
|
||||
|
||||
@PluginBuilderAttribute
|
||||
private String name;
|
||||
|
||||
@PluginConfiguration
|
||||
private Configuration configuration;
|
||||
|
||||
@PluginLoggerContext
|
||||
private LoggerContext loggerContext;
|
||||
|
||||
private Builder() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the profile name or expression.
|
||||
* @param name the profile name or expression
|
||||
* @return this
|
||||
* @see Profiles#of(String...)
|
||||
*/
|
||||
public Builder setName(String name) {
|
||||
this.name = name;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SpringProfileArbiter build() {
|
||||
Environment environment = Log4J2LoggingSystem.getEnvironment(this.loggerContext);
|
||||
if (environment == null) {
|
||||
statusLogger.warn("Cannot create Arbiter, no Spring Environment available");
|
||||
return null;
|
||||
}
|
||||
String name = this.configuration.getStrSubstitutor().replace(this.name);
|
||||
String[] profiles = StringUtils.trimArrayElements(StringUtils.commaDelimitedListToStringArray(name));
|
||||
return new SpringProfileArbiter(environment, profiles);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,82 @@
|
||||
/*
|
||||
* 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 org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.core.LoggerContext;
|
||||
import org.apache.logging.log4j.core.lookup.Interpolator;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.mock.env.MockEnvironment;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
|
||||
|
||||
/**
|
||||
* Tests for {@link SpringEnvironmentLookup}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
class SpringEnvironmentLookupTests {
|
||||
|
||||
private MockEnvironment environment;
|
||||
|
||||
private LoggerContext loggerContext;
|
||||
|
||||
@BeforeEach
|
||||
void setup() {
|
||||
this.environment = new MockEnvironment();
|
||||
this.loggerContext = (LoggerContext) LogManager.getContext(false);
|
||||
this.loggerContext.putObject(Log4J2LoggingSystem.ENVIRONMENT_KEY, this.environment);
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void cleanup() {
|
||||
this.loggerContext.removeObject(Log4J2LoggingSystem.ENVIRONMENT_KEY);
|
||||
}
|
||||
|
||||
@Test
|
||||
void lookupWhenFoundInEnvironmentReturnsValue() {
|
||||
this.environment.setProperty("test", "test");
|
||||
Interpolator lookup = createLookup(this.loggerContext);
|
||||
assertThat(lookup.lookup("spring:test")).isEqualTo("test");
|
||||
}
|
||||
|
||||
@Test
|
||||
void lookupWhenNotFoundInEnvironmentReturnsNull() {
|
||||
Interpolator lookup = createLookup(this.loggerContext);
|
||||
assertThat(lookup.lookup("spring:test")).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void lookupWhenNoSpringEnvironmentThrowsException() {
|
||||
this.loggerContext.removeObject(Log4J2LoggingSystem.ENVIRONMENT_KEY);
|
||||
Interpolator lookup = createLookup(this.loggerContext);
|
||||
assertThatIllegalStateException().isThrownBy(() -> assertThat(lookup.lookup("spring:test")).isEqualTo("test"))
|
||||
.withMessage("Unable to obtain Spring Environment from LoggerContext");
|
||||
}
|
||||
|
||||
private Interpolator createLookup(LoggerContext context) {
|
||||
Interpolator lookup = new Interpolator();
|
||||
lookup.setConfiguration(context.getConfiguration());
|
||||
lookup.setLoggerContext(context);
|
||||
return lookup;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,83 @@
|
||||
/*
|
||||
* 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.Properties;
|
||||
|
||||
import org.apache.logging.log4j.util.PropertiesPropertySource;
|
||||
import org.apache.logging.log4j.util.SystemPropertiesPropertySource;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.mock.env.MockEnvironment;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||
|
||||
/**
|
||||
* Tests for {@link SpringEnvironmentPropertySource}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
class SpringEnvironmentPropertySourceTests {
|
||||
|
||||
private MockEnvironment environment;
|
||||
|
||||
private SpringEnvironmentPropertySource propertySource;
|
||||
|
||||
@BeforeEach
|
||||
void setup() {
|
||||
this.environment = new MockEnvironment();
|
||||
this.environment.setProperty("spring", "boot");
|
||||
this.propertySource = new SpringEnvironmentPropertySource(this.environment);
|
||||
}
|
||||
|
||||
@Test
|
||||
void createWhenEnvironmentIsNullThrowsException() {
|
||||
assertThatIllegalArgumentException().isThrownBy(() -> new SpringEnvironmentPropertySource(null))
|
||||
.withMessage("Environment must not be null");
|
||||
}
|
||||
|
||||
@Test
|
||||
void getPriorityIsOrderedCorrectly() {
|
||||
int priority = this.propertySource.getPriority();
|
||||
assertThat(priority).isEqualTo(-100);
|
||||
assertThat(priority).isLessThan(new SystemPropertiesPropertySource().getPriority());
|
||||
assertThat(priority).isLessThan(new PropertiesPropertySource(new Properties()).getPriority());
|
||||
}
|
||||
|
||||
@Test
|
||||
void getPropertyWhenInEnvironmentReturnsValue() {
|
||||
assertThat(this.propertySource.getProperty("spring")).isEqualTo("boot");
|
||||
}
|
||||
|
||||
@Test
|
||||
void getPropertyWhenNotInEnvironmentReturnsNull() {
|
||||
assertThat(this.propertySource.getProperty("nope")).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void containsPropertyWhenInEnvironmentReturnsTrue() {
|
||||
assertThat(this.propertySource.containsProperty("spring")).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void containsPropertyWhenNotInEnvironmentReturnsFalse() {
|
||||
assertThat(this.propertySource.containsProperty("nope")).isFalse();
|
||||
}
|
||||
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
@ -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>
|
@ -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>
|
@ -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>
|
Loading…
Reference in New Issue