Skip to content

Commit

Permalink
Register Logback's pattern conversion rule using Suppliers
Browse files Browse the repository at this point in the history
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
  • Loading branch information
snicoll committed Jan 8, 2025
1 parent a2d038f commit b84618f
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;
Expand All @@ -38,10 +40,10 @@ class DebugLogbackConfigurator extends LogbackConfigurator {
}

@Override
@SuppressWarnings("rawtypes")
public void conversionRule(String conversionWord, Class<? extends Converter> converterClass) {
<T extends Converter<?>> void conversionRule(String conversionWord, Class<T> converterClass,
Supplier<T> converterSupplier) {
info("Adding conversion rule of type '" + converterClass.getName() + "' for word '" + conversionWord + "'");
super.conversionRule(conversionWord, converterClass);
super.conversionRule(conversionWord, converterClass, converterSupplier);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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}");
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;
Expand Down Expand Up @@ -54,17 +55,18 @@ ReentrantLock getConfigurationLock() {
return this.context.getConfigurationLock();
}

@SuppressWarnings({ "rawtypes", "unchecked" })
void conversionRule(String conversionWord, Class<? extends Converter> converterClass) {
@SuppressWarnings("unchecked")
<T extends Converter<?>> void conversionRule(String conversionWord, Class<T> converterClass,
Supplier<T> converterSupplier) {
Assert.hasLength(conversionWord, "Conversion word must not be empty");
Assert.notNull(converterClass, "Converter class must not be null");
Map<String, String> registry = (Map<String, String>) this.context
.getObject(CoreConstants.PATTERN_RULE_REGISTRY);
Assert.notNull(converterSupplier, "Converter supplier must not be null");
Map<String, Supplier<?>> registry = (Map<String, Supplier<?>>) 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Default logback configuration provided for import
<conversionRule conversionWord="applicationName" class="org.springframework.boot.logging.logback.ApplicationNameConverter"/>
<conversionRule conversionWord="clr" class="org.springframework.boot.logging.logback.ColorConverter"/>
<conversionRule conversionWord="correlationId" class="org.springframework.boot.logging.logback.CorrelationIdConverter"/>
<conversionRule conversionWord="esb" class="org.springframework.boot.logging.logback.EnclosedInSquareBracketsConverter" />
<conversionRule conversionWord="esb" class="org.springframework.boot.logging.logback.EnclosedInSquareBracketsConverter" />
<conversionRule conversionWord="wex" class="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter" />
<conversionRule conversionWord="wEx" class="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter" />

Expand Down

0 comments on commit b84618f

Please sign in to comment.