Skip to content

Commit 825ca76

Browse files
committed
Merge pull request #44467 from nosan
* pr/44467: Close InputStream when loading Log4j2 Configuration from a Resource Closes gh-44467
2 parents 21a5319 + be7e952 commit 825ca76

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystem.java

+10-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
1717
package org.springframework.boot.logging.log4j2;
1818

1919
import java.io.IOException;
20+
import java.io.InputStream;
2021
import java.net.URL;
2122
import java.net.URLConnection;
2223
import java.util.ArrayList;
@@ -278,13 +279,11 @@ protected void loadConfiguration(String location, LogFile logFile, List<String>
278279

279280
private Configuration load(String location, LoggerContext context) throws IOException {
280281
Resource resource = new ApplicationResourceLoader().getResource(location);
281-
ConfigurationSource source = getConfigurationSource(resource);
282-
return ConfigurationFactory.getInstance().getConfiguration(context, source);
283-
}
284-
285-
private ConfigurationSource getConfigurationSource(Resource resource) throws IOException {
282+
ConfigurationFactory factory = ConfigurationFactory.getInstance();
286283
if (resource.isFile()) {
287-
return new ConfigurationSource(resource.getInputStream(), resource.getFile());
284+
try (InputStream inputStream = resource.getInputStream()) {
285+
return factory.getConfiguration(context, new ConfigurationSource(inputStream, resource.getFile()));
286+
}
288287
}
289288
URL url = resource.getURL();
290289
AuthorizationProvider authorizationProvider = ConfigurationFactory
@@ -293,7 +292,10 @@ private ConfigurationSource getConfigurationSource(Resource resource) throws IOE
293292
? SslConfigurationFactory.getSslConfiguration() : null;
294293
URLConnection connection = UrlConnectionFactory.createConnection(url, 0, sslConfiguration,
295294
authorizationProvider);
296-
return new ConfigurationSource(connection.getInputStream(), url, connection.getLastModified());
295+
try (InputStream inputStream = connection.getInputStream()) {
296+
return factory.getConfiguration(context,
297+
new ConfigurationSource(inputStream, url, connection.getLastModified()));
298+
}
297299
}
298300

299301
private CompositeConfiguration createComposite(List<Configuration> configurations) {

0 commit comments

Comments
 (0)