Skip to content

When loading configuration from a Resource, Log4J2LoggingSystem may not close the InputStream #44467

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;
Expand Down Expand Up @@ -278,13 +279,11 @@ protected void loadConfiguration(String location, LogFile logFile, List<String>

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
Expand All @@ -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<Configuration> configurations) {
Expand Down