Skip to content

Commit 0e347af

Browse files
committed
Polish "Add support for optional Log4J2 configuration"
See gh-44488 Signed-off-by: Dmytro Nosan <[email protected]>
1 parent 48154a3 commit 0e347af

File tree

3 files changed

+27
-29
lines changed

3 files changed

+27
-29
lines changed

Diff for: spring-boot-project/spring-boot-docs/src/docs/antora/modules/how-to/pages/logging.adoc

+5-1
Original file line numberDiff line numberDiff line change
@@ -202,4 +202,8 @@ Log4j 2 has support for combining multiple configuration files into a single com
202202
To use this support in Spring Boot, configure configprop:logging.log4j2.config.override[] with the locations of one or more secondary configuration files.
203203
The secondary configuration files will be merged with the primary configuration, whether the primary's source is Spring Boot's defaults, a standard location such as `log4j.xml`, or the location configured by the configprop:logging.config[] property.
204204

205-
NOTE: Log4j2 override configuration file locations can be prefixed with `optional:`, for example, `optional:classpath:log4j2-override.xml`, to indicate that the location is optional and should only be loaded if the resource exists.
205+
[NOTE]
206+
====
207+
Log4j2 override configuration file locations can be prefixed with `optional:`.
208+
For example, `optional:classpath:log4j2-override.xml` indicates that `log4j2-override.xml` should only be loaded if the resource exists.
209+
====

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

+21-27
Original file line numberDiff line numberDiff line change
@@ -274,41 +274,21 @@ protected void loadConfiguration(String location, LogFile logFile, List<String>
274274
List<Configuration> configurations = new ArrayList<>();
275275
LoggerContext context = getLoggerContext();
276276
ResourceLoader resourceLoader = ApplicationResourceLoader.get();
277-
configurations.add(loadConfiguration(resourceLoader, location, context));
277+
configurations.add(load(resourceLoader.getResource(location), context));
278278
for (String override : overrides) {
279-
Configuration overrideConfiguration = loadOptionalConfiguration(resourceLoader, override, context);
279+
Configuration overrideConfiguration = loadOverride(resourceLoader, override, context);
280280
if (overrideConfiguration != null) {
281281
configurations.add(overrideConfiguration);
282282
}
283283
}
284-
context.start(createCompositeConfigurationIfNecessary(configurations));
284+
context.start(mergeConfigurations(configurations));
285285
}
286286
catch (Exception ex) {
287287
throw new IllegalStateException("Could not initialize Log4J2 logging from " + location, ex);
288288
}
289289
}
290290

291-
private Configuration loadOptionalConfiguration(ResourceLoader resourceLoader, String location,
292-
LoggerContext context) throws IOException {
293-
if (location.startsWith(OPTIONAL_PREFIX)) {
294-
Resource resource = resourceLoader.getResource(location.substring(OPTIONAL_PREFIX.length()));
295-
try {
296-
return (resource.exists()) ? loadConfiguration(resource, context) : null;
297-
}
298-
catch (FileNotFoundException ex) {
299-
return null;
300-
}
301-
}
302-
return loadConfiguration(resourceLoader, location, context);
303-
}
304-
305-
private Configuration loadConfiguration(ResourceLoader resourceLoader, String location, LoggerContext context)
306-
throws IOException {
307-
Resource resource = resourceLoader.getResource(location);
308-
return loadConfiguration(resource, context);
309-
}
310-
311-
private Configuration loadConfiguration(Resource resource, LoggerContext context) throws IOException {
291+
private Configuration load(Resource resource, LoggerContext context) throws IOException {
312292
ConfigurationFactory factory = ConfigurationFactory.getInstance();
313293
if (resource.isFile()) {
314294
try (InputStream inputStream = resource.getInputStream()) {
@@ -328,7 +308,21 @@ private Configuration loadConfiguration(Resource resource, LoggerContext context
328308
}
329309
}
330310

331-
private Configuration createCompositeConfigurationIfNecessary(List<Configuration> configurations) {
311+
private Configuration loadOverride(ResourceLoader resourceLoader, String location, LoggerContext context)
312+
throws IOException {
313+
if (location.startsWith(OPTIONAL_PREFIX)) {
314+
Resource resource = resourceLoader.getResource(location.substring(OPTIONAL_PREFIX.length()));
315+
try {
316+
return (resource.exists()) ? load(resource, context) : null;
317+
}
318+
catch (FileNotFoundException ex) {
319+
return null;
320+
}
321+
}
322+
return load(resourceLoader.getResource(location), context);
323+
}
324+
325+
private Configuration mergeConfigurations(List<Configuration> configurations) {
332326
if (configurations.size() == 1) {
333327
return configurations.iterator().next();
334328
}
@@ -354,7 +348,7 @@ private void reinitializeWithOverrides(List<String> overrides) {
354348
ResourceLoader resourceLoader = ApplicationResourceLoader.get();
355349
for (String override : overrides) {
356350
try {
357-
Configuration overrideConfiguration = loadOptionalConfiguration(resourceLoader, override, context);
351+
Configuration overrideConfiguration = loadOverride(resourceLoader, override, context);
358352
if (overrideConfiguration != null) {
359353
configurations.add(overrideConfiguration);
360354
}
@@ -363,7 +357,7 @@ private void reinitializeWithOverrides(List<String> overrides) {
363357
throw new RuntimeException("Failed to load overriding configuration from '" + override + "'", ex);
364358
}
365359
}
366-
context.reconfigure(createCompositeConfigurationIfNecessary(configurations));
360+
context.reconfigure(mergeConfigurations(configurations));
367361
}
368362

369363
@Override

Diff for: spring-boot-project/spring-boot/src/main/resources/META-INF/additional-spring-configuration-metadata.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@
126126
{
127127
"name": "logging.log4j2.config.override",
128128
"type": "java.util.List<java.lang.String>",
129-
"description": "Overriding configuration files used to create a composite configuration."
129+
"description": "Overriding configuration files used to create a composite configuration. Can be prefixed with 'optional:' to only load the override if it exists."
130130
},
131131
{
132132
"name": "logging.logback.rollingpolicy.clean-history-on-start",

0 commit comments

Comments
 (0)