Add Log4J2 PropertySource backed by the Spring Environment
Register a new `PropertySource` when initializing Log4j2 so that properties may be resolved against Spring's Environment. See gh-32733
This commit is contained in:
committed by
Phillip Webb
parent
d21bd822c4
commit
4f8a9441c2
@@ -234,6 +234,7 @@ public class Log4J2LoggingSystem extends AbstractLoggingSystem {
|
||||
}
|
||||
Environment environment = initializationContext.getEnvironment();
|
||||
getLoggerContext().putObjectIfAbsent(ENVIRONMENT_KEY, environment);
|
||||
PropertiesUtil.getProperties().addPropertySource(new SpringPropertySource(environment));
|
||||
loggerContext.getConfiguration().removeFilter(FILTER);
|
||||
super.initialize(initializationContext, configLocation, logFile);
|
||||
markAsInitialized(loggerContext);
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
/**
|
||||
* Returns properties from Spring.
|
||||
*
|
||||
* @author Ralph Goers
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public class SpringPropertySource implements PropertySource {
|
||||
|
||||
private static final int DEFAULT_PRIORITY = -100;
|
||||
|
||||
private final Environment environment;
|
||||
|
||||
public SpringPropertySource(Environment environment) {
|
||||
this.environment = environment;
|
||||
}
|
||||
|
||||
/**
|
||||
* System properties take precedence followed by properties in Log4j properties files.
|
||||
* @return this PropertySource's priority.
|
||||
*/
|
||||
@Override
|
||||
public int getPriority() {
|
||||
return DEFAULT_PRIORITY;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getProperty(String key) {
|
||||
if (this.environment != null) {
|
||||
return this.environment.getProperty(key);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsProperty(String key) {
|
||||
if (this.environment != null) {
|
||||
return this.environment.containsProperty(key);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user