This is an automated email from the ASF dual-hosted git repository. rgoers pushed a commit to branch release-2.x in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git
commit 25fff83746dceabe38aaa026804de8eeb6a161a6 Author: Ralph Goers <[email protected]> AuthorDate: Sun May 15 15:13:35 2022 -0700 LOG4J2-3506 - Support Spring 2.6.x --- .../boot/Log4j2CloudConfigLoggingSystem.java | 63 +++++++++++++++++----- pom.xml | 4 +- 2 files changed, 52 insertions(+), 15 deletions(-) diff --git a/log4j-spring-boot/src/main/java/org/apache/logging/log4j/spring/boot/Log4j2CloudConfigLoggingSystem.java b/log4j-spring-boot/src/main/java/org/apache/logging/log4j/spring/boot/Log4j2CloudConfigLoggingSystem.java index badd9a4c24..5186579d70 100644 --- a/log4j-spring-boot/src/main/java/org/apache/logging/log4j/spring/boot/Log4j2CloudConfigLoggingSystem.java +++ b/log4j-spring-boot/src/main/java/org/apache/logging/log4j/spring/boot/Log4j2CloudConfigLoggingSystem.java @@ -27,6 +27,7 @@ import java.net.URLConnection; import java.net.URLDecoder; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.List; import java.util.Properties; import javax.net.ssl.HttpsURLConnection; @@ -44,9 +45,13 @@ import org.apache.logging.log4j.core.net.ssl.SslConfiguration; import org.apache.logging.log4j.core.net.ssl.SslConfigurationFactory; import org.apache.logging.log4j.core.util.AuthorizationProvider; import org.apache.logging.log4j.core.util.FileUtils; +import org.apache.logging.log4j.internal.LogManagerStatus; import org.apache.logging.log4j.status.StatusLogger; import org.apache.logging.log4j.util.PropertiesUtil; import org.apache.logging.log4j.util.Strings; +import org.springframework.boot.context.properties.bind.BindResult; +import org.springframework.boot.context.properties.bind.Bindable; +import org.springframework.boot.context.properties.bind.Binder; import org.springframework.boot.logging.LogFile; import org.springframework.boot.logging.LoggingInitializationContext; import org.springframework.boot.logging.LoggingSystem; @@ -121,13 +126,35 @@ public class Log4j2CloudConfigLoggingSystem extends Log4J2LoggingSystem { return defaultPath; } + /** + * This method is removed from Spring in 3.x and is scheduled to be removed in 2.8.x. It must be left in + * to support older Spring releases. + * @param location the location + * @param logFile log file configuration + */ @Override protected void loadConfiguration(String location, LogFile logFile) { + loadConfiguration(location, logFile, Collections.emptyList()); + } + + /** + * Added in Spring 2.6.0, the overrides parameter allows override files to be specified in the + * "logging.log4j2.config.override" property. However, spring does not support passing credentials + * when accessing the location. We do. + * @param location The location of the primary configuration. + * @param logFile log file configuration. + * @param overrides Any override files. + */ + @Override + protected void loadConfiguration(String location, LogFile logFile, List<String> overrides) { Assert.notNull(location, "Location must not be null"); try { LoggerContext ctx = getLoggerContext(); - String[] locations = parseConfigLocations(location); - if (locations.length == 1) { + List<String> locations = parseConfigLocations(location); + if (overrides != null) { + locations.addAll(overrides); + } + if (locations.size() == 1) { final URL url = ResourceUtils.getURL(location); final ConfigurationSource source = getConfigurationSource(url); if (source != null) { @@ -135,17 +162,27 @@ public class Log4j2CloudConfigLoggingSystem extends Log4J2LoggingSystem { } } else { final List<AbstractConfiguration> configs = new ArrayList<>(); + boolean first = true; for (final String sourceLocation : locations) { final ConfigurationSource source = getConfigurationSource(ResourceUtils.getURL(sourceLocation)); if (source != null) { - final Configuration config = ConfigurationFactory.getInstance().getConfiguration(ctx, source); - if (config instanceof AbstractConfiguration) { - configs.add((AbstractConfiguration) config); - } else { - LOGGER.warn("Configuration at {} cannot be combined in a CompositeConfiguration", sourceLocation); - return; + try { + final Configuration config = ConfigurationFactory.getInstance().getConfiguration(ctx, source); + if (config instanceof AbstractConfiguration) { + configs.add((AbstractConfiguration) config); + } else { + LOGGER.warn("Configuration at {} cannot be combined in a CompositeConfiguration", sourceLocation); + return; + } + } catch (Exception ex) { + if (!first) { + LOGGER.warn("Error accessing {}: {}. Ignoring override", sourceLocation, ex.getMessage()); + } else { + throw ex; + } } } + first = false; } if (configs.size() > 1) { ctx.start(new CompositeConfiguration(configs)); @@ -156,7 +193,7 @@ public class Log4j2CloudConfigLoggingSystem extends Log4J2LoggingSystem { } catch (Exception ex) { throw new IllegalStateException( - "Could not initialize Log4J2 logging from " + location, ex); + "Could not initialize Log4J2 logging from " + location, ex); } } @@ -166,11 +203,11 @@ public class Log4j2CloudConfigLoggingSystem extends Log4J2LoggingSystem { super.cleanUp(); } - private String[] parseConfigLocations(String configLocations) { + private List<String> parseConfigLocations(String configLocations) { final String[] uris = configLocations.split("\\?"); final List<String> locations = new ArrayList<>(); + locations.add(uris[0]); if (uris.length > 1) { - locations.add(uris[0]); try { final URL url = new URL(configLocations); final String[] pairs = url.getQuery().split("&"); @@ -185,12 +222,12 @@ public class Log4j2CloudConfigLoggingSystem extends Log4J2LoggingSystem { LOGGER.warn("Bad data in configuration string: {}", pair); } } - return locations.toArray(Strings.EMPTY_ARRAY); + return locations; } catch (MalformedURLException ex) { LOGGER.warn("Unable to parse configuration URL {}", configLocations); } } - return new String[] {uris[0]}; + return locations; } private ConfigurationSource getConfigurationSource(URL url) throws IOException, URISyntaxException { diff --git a/pom.xml b/pom.xml index 1683a6a154..3d7ab8af7c 100644 --- a/pom.xml +++ b/pom.xml @@ -240,8 +240,8 @@ <logbackVersion>1.2.11</logbackVersion> <jackson1Version>1.9.13</jackson1Version> <jackson2Version>2.13.2</jackson2Version> - <spring-boot.version>2.5.7</spring-boot.version> - <springVersion>5.3.17</springVersion> + <spring-boot.version>2.6.7</spring-boot.version> + <springVersion>5.3.19</springVersion> <kubernetes-client.version>5.12.1</kubernetes-client.version> <flumeVersion>1.9.0</flumeVersion> <disruptorVersion>3.4.4</disruptorVersion>
