Skip to content

Commit 9377356

Browse files
committed
Add getConfiguration method for multiple URIs
1 parent fe55a68 commit 9377356

File tree

5 files changed

+72
-4
lines changed

5 files changed

+72
-4
lines changed

log4j-core/src/main/java/org/apache/logging/log4j/core/config/ConfigurationFactory.java

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.util.Collections;
2626
import java.util.List;
2727
import java.util.Map;
28+
import java.util.Objects;
2829
import java.util.concurrent.locks.Lock;
2930
import java.util.concurrent.locks.ReentrantLock;
3031
import org.apache.logging.log4j.Level;
@@ -333,6 +334,73 @@ public Configuration getConfiguration(
333334
return getConfiguration(loggerContext, name, configLocation);
334335
}
335336

337+
/**
338+
* Creates a Configuration from multiple configuration URIs.
339+
* If multiple URIs are successfully loaded, they will be combined into a CompositeConfiguration.
340+
*
341+
* @param loggerContext the logger context (may be null)
342+
* @param name the configuration name (may be null)
343+
* @param uris the list of configuration URIs (must not be null or empty)
344+
* @return a Configuration created from the provided URIs
345+
* @throws NullPointerException if uris is null
346+
* @throws IllegalArgumentException if uris is empty
347+
* @throws ConfigurationException if no valid configuration could be created
348+
* from any of the provided URIs
349+
* @since 2.26.0
350+
*/
351+
public Configuration getConfiguration(final LoggerContext loggerContext, final String name, final List<URI> uris) {
352+
353+
Objects.requireNonNull(uris, "uris parameter cannot be null");
354+
355+
if (uris.isEmpty()) {
356+
throw new IllegalArgumentException("URI list cannot be empty");
357+
}
358+
359+
if (uris.size() == 1) {
360+
final Configuration config = getConfiguration(loggerContext, name, uris.get(0));
361+
if (config == null) {
362+
throw new ConfigurationException("Failed to create configuration from: " + uris.get(0));
363+
}
364+
return config;
365+
}
366+
367+
final List<AbstractConfiguration> configurations = new ArrayList<>();
368+
final List<URI> failedUris = new ArrayList<>();
369+
370+
for (final URI uri : uris) {
371+
try {
372+
final Configuration config = getConfiguration(loggerContext, name, uri);
373+
374+
if (config != null) {
375+
if (config instanceof AbstractConfiguration) {
376+
configurations.add((AbstractConfiguration) config);
377+
} else {
378+
LOGGER.error("Configuration at {} is not an AbstractConfiguration", uri);
379+
failedUris.add(uri);
380+
}
381+
} else {
382+
LOGGER.debug("Unable to load configuration from: {}", uri);
383+
failedUris.add(uri);
384+
}
385+
} catch (final Exception ex) {
386+
LOGGER.debug("Error loading configuration from {}: {}", uri, ex.getMessage());
387+
failedUris.add(uri);
388+
}
389+
}
390+
391+
if (configurations.isEmpty()) {
392+
throw new ConfigurationException(String.format(
393+
"Failed to create configuration from any of the %d URIs. Failed URIs: %s",
394+
uris.size(), failedUris));
395+
}
396+
397+
if (!failedUris.isEmpty()) {
398+
LOGGER.warn("Failed to load configurations from: {}", failedUris);
399+
}
400+
401+
return configurations.size() == 1 ? configurations.get(0) : new CompositeConfiguration(configurations);
402+
}
403+
336404
static boolean isClassLoaderUri(final URI uri) {
337405
if (uri == null) {
338406
return false;

log4j-core/src/main/java/org/apache/logging/log4j/core/config/json/package-info.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* Classes and interfaces supporting configuration of Log4j 2 with JSON.
1919
*/
2020
@Export
21-
@Version("2.20.1")
21+
@Version("2.21.0")
2222
package org.apache.logging.log4j.core.config.json;
2323

2424
import org.osgi.annotation.bundle.Export;

log4j-core/src/main/java/org/apache/logging/log4j/core/config/properties/package-info.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* Configuration using Properties files.
1919
*/
2020
@Export
21-
@Version("2.20.1")
21+
@Version("2.21.0")
2222
package org.apache.logging.log4j.core.config.properties;
2323

2424
import org.osgi.annotation.bundle.Export;

log4j-core/src/main/java/org/apache/logging/log4j/core/config/xml/package-info.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* Classes and interfaces supporting configuration of Log4j 2 with XML.
1919
*/
2020
@Export
21-
@Version("2.20.2")
21+
@Version("2.21.0")
2222
package org.apache.logging.log4j.core.config.xml;
2323

2424
import org.osgi.annotation.bundle.Export;

log4j-core/src/main/java/org/apache/logging/log4j/core/config/yaml/package-info.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* Classes and interfaces supporting configuration of Log4j 2 with YAML.
1919
*/
2020
@Export
21-
@Version("2.20.1")
21+
@Version("2.21.0")
2222
package org.apache.logging.log4j.core.config.yaml;
2323

2424
import org.osgi.annotation.bundle.Export;

0 commit comments

Comments
 (0)