From b84618fab962b00e9e697f7c09b01d73120e191c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Nicoll?= Date: Wed, 8 Jan 2025 08:59:51 +0100 Subject: [PATCH] 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 --- .../logback/DebugLogbackConfigurator.java | 10 ++++++---- .../logback/DefaultLogbackConfiguration.java | 15 ++++++++------- .../logging/logback/LogbackConfigurator.java | 18 ++++++++++-------- .../boot/logging/logback/defaults.xml | 2 +- 4 files changed, 25 insertions(+), 20 deletions(-) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/DebugLogbackConfigurator.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/DebugLogbackConfigurator.java index e98296ef6316..3e05b604004b 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/DebugLogbackConfigurator.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/DebugLogbackConfigurator.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,6 +16,8 @@ package org.springframework.boot.logging.logback; +import java.util.function.Supplier; + import ch.qos.logback.classic.Level; import ch.qos.logback.classic.LoggerContext; import ch.qos.logback.classic.spi.ILoggingEvent; @@ -38,10 +40,10 @@ class DebugLogbackConfigurator extends LogbackConfigurator { } @Override - @SuppressWarnings("rawtypes") - public void conversionRule(String conversionWord, Class converterClass) { + > void conversionRule(String conversionWord, Class converterClass, + Supplier converterSupplier) { info("Adding conversion rule of type '" + converterClass.getName() + "' for word '" + conversionWord + "'"); - super.conversionRule(conversionWord, converterClass); + super.conversionRule(conversionWord, converterClass, converterSupplier); } @Override diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/DefaultLogbackConfiguration.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/DefaultLogbackConfiguration.java index c136bd93a74b..6be56b1dd7f5 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/DefaultLogbackConfiguration.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/DefaultLogbackConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2024 the original author or authors. + * Copyright 2012-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -99,11 +99,12 @@ void apply(LogbackConfigurator config) { private void defaults(LogbackConfigurator config) { deprecatedDefaults(config); - config.conversionRule("clr", ColorConverter.class); - config.conversionRule("correlationId", CorrelationIdConverter.class); - config.conversionRule("esb", EnclosedInSquareBracketsConverter.class); - config.conversionRule("wex", WhitespaceThrowableProxyConverter.class); - config.conversionRule("wEx", ExtendedWhitespaceThrowableProxyConverter.class); + config.conversionRule("clr", ColorConverter.class, ColorConverter::new); + config.conversionRule("correlationId", CorrelationIdConverter.class, CorrelationIdConverter::new); + config.conversionRule("esb", EnclosedInSquareBracketsConverter.class, EnclosedInSquareBracketsConverter::new); + config.conversionRule("wex", WhitespaceThrowableProxyConverter.class, WhitespaceThrowableProxyConverter::new); + config.conversionRule("wEx", ExtendedWhitespaceThrowableProxyConverter.class, + ExtendedWhitespaceThrowableProxyConverter::new); putProperty(config, "CONSOLE_LOG_PATTERN", CONSOLE_LOG_PATTERN); putProperty(config, "CONSOLE_LOG_CHARSET", "${CONSOLE_LOG_CHARSET:-" + DEFAULT_CHARSET + "}"); putProperty(config, "CONSOLE_LOG_THRESHOLD", "${CONSOLE_LOG_THRESHOLD:-TRACE}"); @@ -124,7 +125,7 @@ private void defaults(LogbackConfigurator config) { @SuppressWarnings("removal") private void deprecatedDefaults(LogbackConfigurator config) { - config.conversionRule("applicationName", ApplicationNameConverter.class); + config.conversionRule("applicationName", ApplicationNameConverter.class, ApplicationNameConverter::new); } void putProperty(LogbackConfigurator config, String name, String val) { diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/LogbackConfigurator.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/LogbackConfigurator.java index a64f1ce1f54b..9c491c4c7478 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/LogbackConfigurator.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/LogbackConfigurator.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2024 the original author or authors. + * Copyright 2012-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,6 +19,7 @@ import java.util.HashMap; import java.util.Map; import java.util.concurrent.locks.ReentrantLock; +import java.util.function.Supplier; import ch.qos.logback.classic.Level; import ch.qos.logback.classic.Logger; @@ -54,17 +55,18 @@ ReentrantLock getConfigurationLock() { return this.context.getConfigurationLock(); } - @SuppressWarnings({ "rawtypes", "unchecked" }) - void conversionRule(String conversionWord, Class converterClass) { + @SuppressWarnings("unchecked") + > void conversionRule(String conversionWord, Class converterClass, + Supplier converterSupplier) { Assert.hasLength(conversionWord, "Conversion word must not be empty"); - Assert.notNull(converterClass, "Converter class must not be null"); - Map registry = (Map) this.context - .getObject(CoreConstants.PATTERN_RULE_REGISTRY); + Assert.notNull(converterSupplier, "Converter supplier must not be null"); + Map> registry = (Map>) this.context + .getObject(CoreConstants.PATTERN_RULE_REGISTRY_FOR_SUPPLIERS); if (registry == null) { registry = new HashMap<>(); - this.context.putObject(CoreConstants.PATTERN_RULE_REGISTRY, registry); + this.context.putObject(CoreConstants.PATTERN_RULE_REGISTRY_FOR_SUPPLIERS, registry); } - registry.put(conversionWord, converterClass.getName()); + registry.put(conversionWord, converterSupplier); } void appender(String name, Appender appender) { diff --git a/spring-boot-project/spring-boot/src/main/resources/org/springframework/boot/logging/logback/defaults.xml b/spring-boot-project/spring-boot/src/main/resources/org/springframework/boot/logging/logback/defaults.xml index 73e2c20b4c10..c97ca1df3a33 100644 --- a/spring-boot-project/spring-boot/src/main/resources/org/springframework/boot/logging/logback/defaults.xml +++ b/spring-boot-project/spring-boot/src/main/resources/org/springframework/boot/logging/logback/defaults.xml @@ -8,7 +8,7 @@ Default logback configuration provided for import - +