Skip to content

Commit 22ba22c

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

File tree

5 files changed

+46
-4
lines changed

5 files changed

+46
-4
lines changed

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

Lines changed: 42 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,47 @@ 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+
final List<AbstractConfiguration> configurations = new ArrayList<>();
360+
361+
for (final URI uri : uris) {
362+
final Configuration config = getConfiguration(loggerContext, name, uri);
363+
364+
if (config == null) {
365+
throw new ConfigurationException("Failed to create configuration from: " + uri);
366+
}
367+
368+
if (!(config instanceof AbstractConfiguration)) {
369+
throw new ConfigurationException("Configuration at " + uri + " is not an AbstractConfiguration");
370+
}
371+
372+
configurations.add((AbstractConfiguration) config);
373+
}
374+
375+
return configurations.size() == 1 ? configurations.get(0) : new CompositeConfiguration(configurations);
376+
}
377+
336378
static boolean isClassLoaderUri(final URI uri) {
337379
if (uri == null) {
338380
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)