-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Add getConfiguration method for multiple URIs #3921
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
base: 2.x
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -25,8 +25,10 @@ | |||||||||
import java.util.Collections; | ||||||||||
import java.util.List; | ||||||||||
import java.util.Map; | ||||||||||
import java.util.Objects; | ||||||||||
import java.util.concurrent.locks.Lock; | ||||||||||
import java.util.concurrent.locks.ReentrantLock; | ||||||||||
import java.util.stream.Collectors; | ||||||||||
import org.apache.logging.log4j.Level; | ||||||||||
import org.apache.logging.log4j.Logger; | ||||||||||
import org.apache.logging.log4j.core.LoggerContext; | ||||||||||
|
@@ -333,6 +335,71 @@ public Configuration getConfiguration( | |||||||||
return getConfiguration(loggerContext, name, configLocation); | ||||||||||
} | ||||||||||
|
||||||||||
/** | ||||||||||
* {@return a {@link Configuration} created using provided configuration location {@link URI}s} | ||||||||||
* If the provided list of {@code URI}s is null or empty, {@code getConfiguration(loggerContext, name, (URI) null)} will be returned. | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we explain more explicitly the behavior of an empty list?
Suggested change
|
||||||||||
* | ||||||||||
* @param loggerContext a logger context, may be null | ||||||||||
* @param name a configuration name, may be null | ||||||||||
* @param configLocations configuration location {@code URI}s, may be null or empty | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: I’d prefer not to allow |
||||||||||
* @throws ConfigurationException if configuration could not be created | ||||||||||
* | ||||||||||
* @since 2.26.0 | ||||||||||
*/ | ||||||||||
public Configuration getConfiguration( | ||||||||||
final LoggerContext loggerContext, final String name, final List<URI> configLocations) { | ||||||||||
|
||||||||||
// Sanitize URIs | ||||||||||
final List<URI> distinctConfigLocations = configLocations == null | ||||||||||
? Collections.emptyList() | ||||||||||
: configLocations.stream().filter(Objects::nonNull).distinct().collect(Collectors.toList()); | ||||||||||
|
||||||||||
// Short-circuit if provided URIs are null or empty | ||||||||||
if (distinctConfigLocations.isEmpty()) { | ||||||||||
final Configuration config = getConfiguration(loggerContext, name, (URI) null); | ||||||||||
if (config == null) { | ||||||||||
throw new ConfigurationException("Configuration could not be created"); | ||||||||||
} | ||||||||||
return config; | ||||||||||
} | ||||||||||
|
||||||||||
// Short-circuit if there is only a single URI | ||||||||||
if (distinctConfigLocations.size() == 1) { | ||||||||||
final URI configLocation = distinctConfigLocations.get(0); | ||||||||||
final Configuration config = getConfiguration(loggerContext, name, configLocation); | ||||||||||
if (config == null) { | ||||||||||
final String message = | ||||||||||
String.format("Configuration could not be created from location: `%s`", configLocation); | ||||||||||
throw new ConfigurationException(message); | ||||||||||
} | ||||||||||
return config; | ||||||||||
} | ||||||||||
|
||||||||||
// Create individual configurations | ||||||||||
final List<AbstractConfiguration> configs = distinctConfigLocations.stream() | ||||||||||
.map(configLocation -> { | ||||||||||
final Configuration config = getConfiguration(loggerContext, name, configLocation); | ||||||||||
if (config == null) { | ||||||||||
final String message = | ||||||||||
String.format("Configuration could not be created from location: `%s`", configLocation); | ||||||||||
throw new ConfigurationException(message); | ||||||||||
} | ||||||||||
if (!(config instanceof AbstractConfiguration)) { | ||||||||||
final String message = String.format( | ||||||||||
"Configuration created from location `%s` was expected to be of type `%s`, found: `%s`", | ||||||||||
configLocation, | ||||||||||
AbstractConfiguration.class.getCanonicalName(), | ||||||||||
config.getClass().getCanonicalName()); | ||||||||||
throw new ConfigurationException(message); | ||||||||||
} | ||||||||||
return (AbstractConfiguration) config; | ||||||||||
}) | ||||||||||
.collect(Collectors.toList()); | ||||||||||
|
||||||||||
// Combine created configurations | ||||||||||
return new CompositeConfiguration(configs); | ||||||||||
} | ||||||||||
|
||||||||||
static boolean isClassLoaderUri(final URI uri) { | ||||||||||
if (uri == null) { | ||||||||||
return false; | ||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<entry xmlns="https://logging.apache.org/xml/ns" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation=" | ||
https://logging.apache.org/xml/ns | ||
https://logging.apache.org/xml/ns/log4j-changelog-0.xsd" | ||
type="added"> | ||
<issue id="3775" link="https://github.com/apache/logging-log4j2/issues/3775"/> | ||
vy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
<issue id="3921" link="https://github.com/apache/logging-log4j2/pull/3921"/> | ||
<description format="asciidoc"> | ||
Add a new `ConfigurationFactory::getConfiguration` method accepting multiple `URI`s | ||
</description> | ||
</entry> |
Uh oh!
There was an error while loading. Please reload this page.