From 4f8a9441c2d9668b3ceb5cfcd862c0a424248542 Mon Sep 17 00:00:00 2001 From: Ralph Goers Date: Wed, 12 Oct 2022 17:24:51 -0700 Subject: [PATCH] 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 --- .../logging/log4j2/Log4J2LoggingSystem.java | 1 + .../logging/log4j2/SpringPropertySource.java | 64 +++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/SpringPropertySource.java diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystem.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystem.java index b1ec7c67a1..00c362e1b3 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystem.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystem.java @@ -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); diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/SpringPropertySource.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/SpringPropertySource.java new file mode 100644 index 0000000000..ea366d993f --- /dev/null +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/SpringPropertySource.java @@ -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; + } + +}