Skip to content

Commit b84618f

Browse files
committed
Register Logback's pattern conversion rule using Suppliers
This commit makes use of a feature introduced in LogBack 1.5.15 that allows converter to be specified using a supplier rather than a fully qualified class name. Closes gh-43588
1 parent a2d038f commit b84618f

File tree

4 files changed

+25
-20
lines changed

4 files changed

+25
-20
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/DebugLogbackConfigurator.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.boot.logging.logback;
1818

19+
import java.util.function.Supplier;
20+
1921
import ch.qos.logback.classic.Level;
2022
import ch.qos.logback.classic.LoggerContext;
2123
import ch.qos.logback.classic.spi.ILoggingEvent;
@@ -38,10 +40,10 @@ class DebugLogbackConfigurator extends LogbackConfigurator {
3840
}
3941

4042
@Override
41-
@SuppressWarnings("rawtypes")
42-
public void conversionRule(String conversionWord, Class<? extends Converter> converterClass) {
43+
<T extends Converter<?>> void conversionRule(String conversionWord, Class<T> converterClass,
44+
Supplier<T> converterSupplier) {
4345
info("Adding conversion rule of type '" + converterClass.getName() + "' for word '" + conversionWord + "'");
44-
super.conversionRule(conversionWord, converterClass);
46+
super.conversionRule(conversionWord, converterClass, converterSupplier);
4547
}
4648

4749
@Override

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/DefaultLogbackConfiguration.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -99,11 +99,12 @@ void apply(LogbackConfigurator config) {
9999

100100
private void defaults(LogbackConfigurator config) {
101101
deprecatedDefaults(config);
102-
config.conversionRule("clr", ColorConverter.class);
103-
config.conversionRule("correlationId", CorrelationIdConverter.class);
104-
config.conversionRule("esb", EnclosedInSquareBracketsConverter.class);
105-
config.conversionRule("wex", WhitespaceThrowableProxyConverter.class);
106-
config.conversionRule("wEx", ExtendedWhitespaceThrowableProxyConverter.class);
102+
config.conversionRule("clr", ColorConverter.class, ColorConverter::new);
103+
config.conversionRule("correlationId", CorrelationIdConverter.class, CorrelationIdConverter::new);
104+
config.conversionRule("esb", EnclosedInSquareBracketsConverter.class, EnclosedInSquareBracketsConverter::new);
105+
config.conversionRule("wex", WhitespaceThrowableProxyConverter.class, WhitespaceThrowableProxyConverter::new);
106+
config.conversionRule("wEx", ExtendedWhitespaceThrowableProxyConverter.class,
107+
ExtendedWhitespaceThrowableProxyConverter::new);
107108
putProperty(config, "CONSOLE_LOG_PATTERN", CONSOLE_LOG_PATTERN);
108109
putProperty(config, "CONSOLE_LOG_CHARSET", "${CONSOLE_LOG_CHARSET:-" + DEFAULT_CHARSET + "}");
109110
putProperty(config, "CONSOLE_LOG_THRESHOLD", "${CONSOLE_LOG_THRESHOLD:-TRACE}");
@@ -124,7 +125,7 @@ private void defaults(LogbackConfigurator config) {
124125

125126
@SuppressWarnings("removal")
126127
private void deprecatedDefaults(LogbackConfigurator config) {
127-
config.conversionRule("applicationName", ApplicationNameConverter.class);
128+
config.conversionRule("applicationName", ApplicationNameConverter.class, ApplicationNameConverter::new);
128129
}
129130

130131
void putProperty(LogbackConfigurator config, String name, String val) {

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/LogbackConfigurator.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@
1919
import java.util.HashMap;
2020
import java.util.Map;
2121
import java.util.concurrent.locks.ReentrantLock;
22+
import java.util.function.Supplier;
2223

2324
import ch.qos.logback.classic.Level;
2425
import ch.qos.logback.classic.Logger;
@@ -54,17 +55,18 @@ ReentrantLock getConfigurationLock() {
5455
return this.context.getConfigurationLock();
5556
}
5657

57-
@SuppressWarnings({ "rawtypes", "unchecked" })
58-
void conversionRule(String conversionWord, Class<? extends Converter> converterClass) {
58+
@SuppressWarnings("unchecked")
59+
<T extends Converter<?>> void conversionRule(String conversionWord, Class<T> converterClass,
60+
Supplier<T> converterSupplier) {
5961
Assert.hasLength(conversionWord, "Conversion word must not be empty");
60-
Assert.notNull(converterClass, "Converter class must not be null");
61-
Map<String, String> registry = (Map<String, String>) this.context
62-
.getObject(CoreConstants.PATTERN_RULE_REGISTRY);
62+
Assert.notNull(converterSupplier, "Converter supplier must not be null");
63+
Map<String, Supplier<?>> registry = (Map<String, Supplier<?>>) this.context
64+
.getObject(CoreConstants.PATTERN_RULE_REGISTRY_FOR_SUPPLIERS);
6365
if (registry == null) {
6466
registry = new HashMap<>();
65-
this.context.putObject(CoreConstants.PATTERN_RULE_REGISTRY, registry);
67+
this.context.putObject(CoreConstants.PATTERN_RULE_REGISTRY_FOR_SUPPLIERS, registry);
6668
}
67-
registry.put(conversionWord, converterClass.getName());
69+
registry.put(conversionWord, converterSupplier);
6870
}
6971

7072
void appender(String name, Appender<?> appender) {

spring-boot-project/spring-boot/src/main/resources/org/springframework/boot/logging/logback/defaults.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Default logback configuration provided for import
88
<conversionRule conversionWord="applicationName" class="org.springframework.boot.logging.logback.ApplicationNameConverter"/>
99
<conversionRule conversionWord="clr" class="org.springframework.boot.logging.logback.ColorConverter"/>
1010
<conversionRule conversionWord="correlationId" class="org.springframework.boot.logging.logback.CorrelationIdConverter"/>
11-
<conversionRule conversionWord="esb" class="org.springframework.boot.logging.logback.EnclosedInSquareBracketsConverter" />
11+
<conversionRule conversionWord="esb" class="org.springframework.boot.logging.logback.EnclosedInSquareBracketsConverter" />
1212
<conversionRule conversionWord="wex" class="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter" />
1313
<conversionRule conversionWord="wEx" class="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter" />
1414

0 commit comments

Comments
 (0)