From 5fcf95448dcff4107cc66907a0ae74a0fa29e037 Mon Sep 17 00:00:00 2001 From: Dmytro Nosan Date: Thu, 27 Feb 2025 20:40:47 +0200 Subject: [PATCH] Close InputStream when loading Log4j2 Configuration from a Resource Signed-off-by: Dmytro Nosan --- .../logging/log4j2/Log4J2LoggingSystem.java | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) 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 21d4d47a3971..e58235620e4d 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 @@ -1,5 +1,5 @@ /* - * Copyright 2012-2024 the original author or authors. + * Copyright 2012-2025 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. @@ -17,6 +17,7 @@ package org.springframework.boot.logging.log4j2; import java.io.IOException; +import java.io.InputStream; import java.net.URL; import java.net.URLConnection; import java.util.ArrayList; @@ -278,13 +279,11 @@ protected void loadConfiguration(String location, LogFile logFile, List private Configuration load(String location, LoggerContext context) throws IOException { Resource resource = new ApplicationResourceLoader().getResource(location); - ConfigurationSource source = getConfigurationSource(resource); - return ConfigurationFactory.getInstance().getConfiguration(context, source); - } - - private ConfigurationSource getConfigurationSource(Resource resource) throws IOException { + ConfigurationFactory factory = ConfigurationFactory.getInstance(); if (resource.isFile()) { - return new ConfigurationSource(resource.getInputStream(), resource.getFile()); + try (InputStream inputStream = resource.getInputStream()) { + return factory.getConfiguration(context, new ConfigurationSource(inputStream, resource.getFile())); + } } URL url = resource.getURL(); AuthorizationProvider authorizationProvider = ConfigurationFactory @@ -293,7 +292,10 @@ private ConfigurationSource getConfigurationSource(Resource resource) throws IOE ? SslConfigurationFactory.getSslConfiguration() : null; URLConnection connection = UrlConnectionFactory.createConnection(url, 0, sslConfiguration, authorizationProvider); - return new ConfigurationSource(connection.getInputStream(), url, connection.getLastModified()); + try (InputStream inputStream = connection.getInputStream()) { + return factory.getConfiguration(context, + new ConfigurationSource(inputStream, url, connection.getLastModified())); + } } private CompositeConfiguration createComposite(List configurations) {