Skip to content

Commit

Permalink
rename getDefaultConverterMap as getDefaultConverterSupplierMap, add …
Browse files Browse the repository at this point in the history
…getDefaultConverterMap method returning Map<String, String> to ensure backward compatibility

Signed-off-by: Ceki Gulcu <ceki@qos.ch>
  • Loading branch information
ceki committed Oct 29, 2024
1 parent 9201136 commit 49f0638
Show file tree
Hide file tree
Showing 9 changed files with 212 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import ch.qos.logback.classic.pattern.color.HighlightingCompositeConverter;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.CoreConstants;
import ch.qos.logback.core.pattern.Converter;
import ch.qos.logback.core.pattern.DynamicConverter;
import ch.qos.logback.core.pattern.PatternLayoutBase;
import ch.qos.logback.core.pattern.color.*;
Expand All @@ -41,137 +40,156 @@

public class PatternLayout extends PatternLayoutBase<ILoggingEvent> {

public static final Map<String, Supplier<DynamicConverter>> DEFAULT_CONVERTER_MAP = new HashMap<>();
public static final Map<String, Supplier<DynamicConverter>> DEFAULT_CONVERTER_SUPPLIER_MAP = new HashMap<>();

public static final Map<String, String> DEFAULT_CONVERTER_MAP = new HashMap<>();
public static final Map<String, String> CONVERTER_CLASS_TO_KEY_MAP = new HashMap<String, String>();

/**
* @deprecated replaced by DEFAULT_CONVERTER_MAP
*/
public static final Map<String, Supplier<DynamicConverter>> defaultConverterMap = DEFAULT_CONVERTER_MAP;
@Deprecated
public static final Map<String, String> defaultConverterMap = DEFAULT_CONVERTER_MAP;

public static final String HEADER_PREFIX = "#logback.classic pattern: ";

static {
DEFAULT_CONVERTER_MAP.putAll(Parser.DEFAULT_COMPOSITE_CONVERTER_MAP);
DEFAULT_CONVERTER_SUPPLIER_MAP.putAll(Parser.DEFAULT_COMPOSITE_CONVERTER_MAP);

DEFAULT_CONVERTER_MAP.put("d", DateConverter::new);
DEFAULT_CONVERTER_MAP.put("date", DateConverter::new);
DEFAULT_CONVERTER_SUPPLIER_MAP.put("d", DateConverter::new);
DEFAULT_CONVERTER_SUPPLIER_MAP.put("date", DateConverter::new);
// used by PrefixComposite converter
CONVERTER_CLASS_TO_KEY_MAP.put(DateConverter.class.getName(), "date");

DEFAULT_CONVERTER_MAP.put("ms", MicrosecondConverter::new);
DEFAULT_CONVERTER_MAP.put("micros", MicrosecondConverter::new);
DEFAULT_CONVERTER_SUPPLIER_MAP.put("ms", MicrosecondConverter::new);
DEFAULT_CONVERTER_SUPPLIER_MAP.put("micros", MicrosecondConverter::new);
CONVERTER_CLASS_TO_KEY_MAP.put(MicrosecondConverter.class.getName(), "micros");

DEFAULT_CONVERTER_MAP.put("r", RelativeTimeConverter::new);
DEFAULT_CONVERTER_MAP.put("relative", RelativeTimeConverter::new);
DEFAULT_CONVERTER_SUPPLIER_MAP.put("r", RelativeTimeConverter::new);
DEFAULT_CONVERTER_SUPPLIER_MAP.put("relative", RelativeTimeConverter::new);
CONVERTER_CLASS_TO_KEY_MAP.put(RelativeTimeConverter.class.getName(), "relative");

DEFAULT_CONVERTER_MAP.put("level", LevelConverter::new);
DEFAULT_CONVERTER_MAP.put("le", LevelConverter::new);
DEFAULT_CONVERTER_MAP.put("p", LevelConverter::new);
DEFAULT_CONVERTER_SUPPLIER_MAP.put("level", LevelConverter::new);
DEFAULT_CONVERTER_SUPPLIER_MAP.put("le", LevelConverter::new);
DEFAULT_CONVERTER_SUPPLIER_MAP.put("p", LevelConverter::new);
CONVERTER_CLASS_TO_KEY_MAP.put(LevelConverter.class.getName(), "level");

DEFAULT_CONVERTER_MAP.put("t", ThreadConverter::new);
DEFAULT_CONVERTER_MAP.put("thread", ThreadConverter::new);
DEFAULT_CONVERTER_SUPPLIER_MAP.put("t", ThreadConverter::new);
DEFAULT_CONVERTER_SUPPLIER_MAP.put("thread", ThreadConverter::new);
CONVERTER_CLASS_TO_KEY_MAP.put(ThreadConverter.class.getName(), "thread");

DEFAULT_CONVERTER_MAP.put("lo", LoggerConverter::new);
DEFAULT_CONVERTER_MAP.put("logger", LoggerConverter::new);
DEFAULT_CONVERTER_MAP.put("c", LoggerConverter::new);
DEFAULT_CONVERTER_SUPPLIER_MAP.put("lo", LoggerConverter::new);
DEFAULT_CONVERTER_SUPPLIER_MAP.put("logger", LoggerConverter::new);
DEFAULT_CONVERTER_SUPPLIER_MAP.put("c", LoggerConverter::new);
CONVERTER_CLASS_TO_KEY_MAP.put(LoggerConverter.class.getName(), "logger");

DEFAULT_CONVERTER_MAP.put("m", MessageConverter::new);
DEFAULT_CONVERTER_MAP.put("msg", MessageConverter::new);
DEFAULT_CONVERTER_MAP.put("message", MessageConverter::new);
DEFAULT_CONVERTER_SUPPLIER_MAP.put("m", MessageConverter::new);
DEFAULT_CONVERTER_SUPPLIER_MAP.put("msg", MessageConverter::new);
DEFAULT_CONVERTER_SUPPLIER_MAP.put("message", MessageConverter::new);
CONVERTER_CLASS_TO_KEY_MAP.put(MessageConverter.class.getName(), "message");

DEFAULT_CONVERTER_MAP.put("C", ClassOfCallerConverter::new);
DEFAULT_CONVERTER_MAP.put("class", ClassOfCallerConverter::new);
DEFAULT_CONVERTER_SUPPLIER_MAP.put("C", ClassOfCallerConverter::new);
DEFAULT_CONVERTER_SUPPLIER_MAP.put("class", ClassOfCallerConverter::new);
CONVERTER_CLASS_TO_KEY_MAP.put(ClassOfCallerConverter.class.getName(), "class");

DEFAULT_CONVERTER_MAP.put("M", MethodOfCallerConverter::new);
DEFAULT_CONVERTER_MAP.put("method", MethodOfCallerConverter::new);
DEFAULT_CONVERTER_SUPPLIER_MAP.put("M", MethodOfCallerConverter::new);
DEFAULT_CONVERTER_SUPPLIER_MAP.put("method", MethodOfCallerConverter::new);
CONVERTER_CLASS_TO_KEY_MAP.put(MethodOfCallerConverter.class.getName(), "method");

DEFAULT_CONVERTER_MAP.put("L", LineOfCallerConverter::new);
DEFAULT_CONVERTER_MAP.put("line", LineOfCallerConverter::new);
DEFAULT_CONVERTER_SUPPLIER_MAP.put("L", LineOfCallerConverter::new);
DEFAULT_CONVERTER_SUPPLIER_MAP.put("line", LineOfCallerConverter::new);
CONVERTER_CLASS_TO_KEY_MAP.put(LineOfCallerConverter.class.getName(), "line");

DEFAULT_CONVERTER_MAP.put("F", FileOfCallerConverter::new);
DEFAULT_CONVERTER_MAP.put("file", FileOfCallerConverter::new);
DEFAULT_CONVERTER_SUPPLIER_MAP.put("F", FileOfCallerConverter::new);
DEFAULT_CONVERTER_SUPPLIER_MAP.put("file", FileOfCallerConverter::new);
CONVERTER_CLASS_TO_KEY_MAP.put(FileOfCallerConverter.class.getName(), "file");

DEFAULT_CONVERTER_MAP.put("X", MDCConverter::new);
DEFAULT_CONVERTER_MAP.put("mdc", MDCConverter::new);
DEFAULT_CONVERTER_SUPPLIER_MAP.put("X", MDCConverter::new);
DEFAULT_CONVERTER_SUPPLIER_MAP.put("mdc", MDCConverter::new);

DEFAULT_CONVERTER_MAP.put("ex", ThrowableProxyConverter::new);
DEFAULT_CONVERTER_MAP.put("exception", ThrowableProxyConverter::new);
DEFAULT_CONVERTER_MAP.put("rEx", RootCauseFirstThrowableProxyConverter::new);
DEFAULT_CONVERTER_MAP.put("rootException", RootCauseFirstThrowableProxyConverter::new);
DEFAULT_CONVERTER_MAP.put("throwable", ThrowableProxyConverter::new);
DEFAULT_CONVERTER_SUPPLIER_MAP.put("ex", ThrowableProxyConverter::new);
DEFAULT_CONVERTER_SUPPLIER_MAP.put("exception", ThrowableProxyConverter::new);
DEFAULT_CONVERTER_SUPPLIER_MAP.put("rEx", RootCauseFirstThrowableProxyConverter::new);
DEFAULT_CONVERTER_SUPPLIER_MAP.put("rootException", RootCauseFirstThrowableProxyConverter::new);
DEFAULT_CONVERTER_SUPPLIER_MAP.put("throwable", ThrowableProxyConverter::new);

DEFAULT_CONVERTER_MAP.put("xEx", ExtendedThrowableProxyConverter::new);
DEFAULT_CONVERTER_MAP.put("xException", ExtendedThrowableProxyConverter::new);
DEFAULT_CONVERTER_MAP.put("xThrowable", ExtendedThrowableProxyConverter::new);
DEFAULT_CONVERTER_SUPPLIER_MAP.put("xEx", ExtendedThrowableProxyConverter::new);
DEFAULT_CONVERTER_SUPPLIER_MAP.put("xException", ExtendedThrowableProxyConverter::new);
DEFAULT_CONVERTER_SUPPLIER_MAP.put("xThrowable", ExtendedThrowableProxyConverter::new);

DEFAULT_CONVERTER_MAP.put("nopex", NopThrowableInformationConverter::new);
DEFAULT_CONVERTER_MAP.put("nopexception", NopThrowableInformationConverter::new);
DEFAULT_CONVERTER_SUPPLIER_MAP.put("nopex", NopThrowableInformationConverter::new);
DEFAULT_CONVERTER_SUPPLIER_MAP.put("nopexception", NopThrowableInformationConverter::new);

DEFAULT_CONVERTER_MAP.put("cn", ContextNameConverter::new);
DEFAULT_CONVERTER_MAP.put("contextName", ContextNameConverter::new);
DEFAULT_CONVERTER_SUPPLIER_MAP.put("cn", ContextNameConverter::new);
DEFAULT_CONVERTER_SUPPLIER_MAP.put("contextName", ContextNameConverter::new);
CONVERTER_CLASS_TO_KEY_MAP.put(ContextNameConverter.class.getName(), "contextName");

DEFAULT_CONVERTER_MAP.put("caller", CallerDataConverter::new);
DEFAULT_CONVERTER_SUPPLIER_MAP.put("caller", CallerDataConverter::new);
CONVERTER_CLASS_TO_KEY_MAP.put(CallerDataConverter.class.getName(), "caller");

DEFAULT_CONVERTER_MAP.put("marker", MarkerConverter::new);
DEFAULT_CONVERTER_SUPPLIER_MAP.put("marker", MarkerConverter::new);
CONVERTER_CLASS_TO_KEY_MAP.put(MarkerConverter.class.getName(), "marker");

DEFAULT_CONVERTER_MAP.put("kvp", KeyValuePairConverter::new);
DEFAULT_CONVERTER_SUPPLIER_MAP.put("kvp", KeyValuePairConverter::new);
CONVERTER_CLASS_TO_KEY_MAP.put(KeyValuePairConverter.class.getName(), "kvp");

DEFAULT_CONVERTER_MAP.put("maskedKvp", MaskedKeyValuePairConverter::new);
DEFAULT_CONVERTER_SUPPLIER_MAP.put("maskedKvp", MaskedKeyValuePairConverter::new);
CONVERTER_CLASS_TO_KEY_MAP.put(MaskedKeyValuePairConverter.class.getName(), "maskedKvp");

DEFAULT_CONVERTER_MAP.put("property", PropertyConverter::new);

DEFAULT_CONVERTER_MAP.put("n", LineSeparatorConverter::new);

DEFAULT_CONVERTER_MAP.put("black", BlackCompositeConverter::new);
DEFAULT_CONVERTER_MAP.put("red", RedCompositeConverter::new);
DEFAULT_CONVERTER_MAP.put("green", GreenCompositeConverter::new);
DEFAULT_CONVERTER_MAP.put("yellow", YellowCompositeConverter::new);
DEFAULT_CONVERTER_MAP.put("blue", BlueCompositeConverter::new);
DEFAULT_CONVERTER_MAP.put("magenta", MagentaCompositeConverter::new);
DEFAULT_CONVERTER_MAP.put("cyan", CyanCompositeConverter::new);
DEFAULT_CONVERTER_MAP.put("white", WhiteCompositeConverter::new);
DEFAULT_CONVERTER_MAP.put("gray", GrayCompositeConverter::new);
DEFAULT_CONVERTER_MAP.put("boldRed", BoldRedCompositeConverter::new);
DEFAULT_CONVERTER_MAP.put("boldGreen", BoldGreenCompositeConverter::new);
DEFAULT_CONVERTER_MAP.put("boldYellow", BoldYellowCompositeConverter::new);
DEFAULT_CONVERTER_MAP.put("boldBlue", BoldBlueCompositeConverter::new);
DEFAULT_CONVERTER_MAP.put("boldMagenta", BoldMagentaCompositeConverter::new);
DEFAULT_CONVERTER_MAP.put("boldCyan", BoldCyanCompositeConverter::new);
DEFAULT_CONVERTER_MAP.put("boldWhite", BoldWhiteCompositeConverter::new);
DEFAULT_CONVERTER_MAP.put("highlight", HighlightingCompositeConverter::new);

DEFAULT_CONVERTER_MAP.put("lsn", LocalSequenceNumberConverter::new);
DEFAULT_CONVERTER_SUPPLIER_MAP.put("property", PropertyConverter::new);

DEFAULT_CONVERTER_SUPPLIER_MAP.put("n", LineSeparatorConverter::new);

DEFAULT_CONVERTER_SUPPLIER_MAP.put("black", BlackCompositeConverter::new);
DEFAULT_CONVERTER_SUPPLIER_MAP.put("red", RedCompositeConverter::new);
DEFAULT_CONVERTER_SUPPLIER_MAP.put("green", GreenCompositeConverter::new);
DEFAULT_CONVERTER_SUPPLIER_MAP.put("yellow", YellowCompositeConverter::new);
DEFAULT_CONVERTER_SUPPLIER_MAP.put("blue", BlueCompositeConverter::new);
DEFAULT_CONVERTER_SUPPLIER_MAP.put("magenta", MagentaCompositeConverter::new);
DEFAULT_CONVERTER_SUPPLIER_MAP.put("cyan", CyanCompositeConverter::new);
DEFAULT_CONVERTER_SUPPLIER_MAP.put("white", WhiteCompositeConverter::new);
DEFAULT_CONVERTER_SUPPLIER_MAP.put("gray", GrayCompositeConverter::new);
DEFAULT_CONVERTER_SUPPLIER_MAP.put("boldRed", BoldRedCompositeConverter::new);
DEFAULT_CONVERTER_SUPPLIER_MAP.put("boldGreen", BoldGreenCompositeConverter::new);
DEFAULT_CONVERTER_SUPPLIER_MAP.put("boldYellow", BoldYellowCompositeConverter::new);
DEFAULT_CONVERTER_SUPPLIER_MAP.put("boldBlue", BoldBlueCompositeConverter::new);
DEFAULT_CONVERTER_SUPPLIER_MAP.put("boldMagenta", BoldMagentaCompositeConverter::new);
DEFAULT_CONVERTER_SUPPLIER_MAP.put("boldCyan", BoldCyanCompositeConverter::new);
DEFAULT_CONVERTER_SUPPLIER_MAP.put("boldWhite", BoldWhiteCompositeConverter::new);
DEFAULT_CONVERTER_SUPPLIER_MAP.put("highlight", HighlightingCompositeConverter::new);

DEFAULT_CONVERTER_SUPPLIER_MAP.put("lsn", LocalSequenceNumberConverter::new);
CONVERTER_CLASS_TO_KEY_MAP.put(LocalSequenceNumberConverter.class.getName(), "lsn");

DEFAULT_CONVERTER_MAP.put("sn", SequenceNumberConverter::new);
DEFAULT_CONVERTER_MAP.put("sequenceNumber", SequenceNumberConverter::new);
DEFAULT_CONVERTER_SUPPLIER_MAP.put("sn", SequenceNumberConverter::new);
DEFAULT_CONVERTER_SUPPLIER_MAP.put("sequenceNumber", SequenceNumberConverter::new);
CONVERTER_CLASS_TO_KEY_MAP.put(SequenceNumberConverter.class.getName(), "sequenceNumber");

DEFAULT_CONVERTER_MAP.put("prefix", PrefixCompositeConverter::new);
DEFAULT_CONVERTER_SUPPLIER_MAP.put("prefix", PrefixCompositeConverter::new);

}

public PatternLayout() {
this.postCompileProcessor = new EnsureExceptionHandling();
}

public Map<String, Supplier<DynamicConverter>> getDefaultConverterMap() {
public Map<String, Supplier<DynamicConverter>> getDefaultConverterSupplierMap() {
return DEFAULT_CONVERTER_SUPPLIER_MAP;
}

/**
* <p>BEWARE: The map of type String,String for mapping conversion words is deprecated.
* Use {@link #getDefaultConverterSupplierMap()} instead.</p>
*
* <p>Existing code such as getDefaultMap().put("k", X.class.getName()) should be replaced by
* getDefaultConverterSupplierMap().put("k", X::new) </p>
*
* <p>Note that values in the map will still be taken into account and processed correctly.</p>
*
* @return a map of keys and class names
*/
@Deprecated
public Map<String, String> getDefaultConverterMap() {
return DEFAULT_CONVERTER_MAP;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,9 @@ public void start() {
}
}

protected Map<String, Supplier<DynamicConverter>> getDefaultConverterMap() {
return PatternLayout.DEFAULT_CONVERTER_MAP;
@Override
protected Map<String, Supplier<DynamicConverter>> getDefaultConverterSupplierMap() {
return PatternLayout.DEFAULT_CONVERTER_SUPPLIER_MAP;
}

public String doLayout(ILoggingEvent event) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import ch.qos.logback.core.pattern.DynamicConverter;

/**
* This class serves the super-class of all converters in logback. It extends
* This class serves the super-class of almost all converters in logback-classic. It extends
* {@link DynamicConverter}.
*
* @author Ceki Gulcu
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,16 @@

/**
* Test backward compatibility support by virtue of correct compilation.
*
* See also SubPatternLayoutTest
*/
public class SubPatternLayout extends PatternLayout {

static String DOOO = "dooo";

SubPatternLayout() {
Map<String, String> defaultConverterMap = getDefaultConverterMap();
defaultConverterMap.put("dooo", DateConverter.class.getName());
defaultConverterMap.put(DOOO, DateConverter.class.getName());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Logback: the reliable, generic, fast and flexible logging framework.
* Copyright (C) 1999-2024, QOS.ch. All rights reserved.
*
* This program and the accompanying materials are dual-licensed under
* either the terms of the Eclipse Public License v1.0 as published by
* the Eclipse Foundation
*
* or (per the licensee's choosing)
*
* under the terms of the GNU Lesser General Public License version 2.1
* as published by the Free Software Foundation.
*/

package ch.qos.logback.classic.pattern;

import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.spi.LoggingEvent;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class SubPatternLayoutTest {

LoggerContext context = new LoggerContext();

@Test public void smoke() {
SubPatternLayout layout = new SubPatternLayout();
layout.setPattern("%"+SubPatternLayout.DOOO);
layout.setContext(context);
layout.start();
LoggingEvent event = new LoggingEvent();
event.setTimeStamp(0);

String result = layout.doLayout(event);
assertEquals("1970-01-01 01:00:00,000", result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public void start() {
}
}

protected abstract Map<String, Supplier<DynamicConverter>> getDefaultConverterMap();
protected abstract Map<String, Supplier<DynamicConverter>> getDefaultConverterSupplierMap();

/**
* Returns a map where the default converter map is merged with the map
Expand All @@ -107,9 +107,9 @@ public Map<String, Supplier<DynamicConverter>> getEffectiveConverterMap() {
Map<String, Supplier<DynamicConverter>> effectiveMap = new HashMap<>();

// add the least specific map fist
Map<String, Supplier<DynamicConverter>> defaultMap = getDefaultConverterMap();
if (defaultMap != null) {
effectiveMap.putAll(defaultMap);
Map<String, Supplier<DynamicConverter>> defaultSupplierMap = getDefaultConverterSupplierMap();
if (defaultSupplierMap != null) {
effectiveMap.putAll(defaultSupplierMap);
}

// contextMap is more specific than the default map
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@
import ch.qos.logback.core.spi.LifeCycle;
import ch.qos.logback.core.status.Status;

/**
* As the name suggests, a DynamicConverter performs a conversion based on the parameter E
* given to the {@link #convert(E)} method. Almost all converters are derived from the
* DynamicConverter class.
*
* @param <E>
*/
abstract public class DynamicConverter<E> extends FormattingConverter<E> implements LifeCycle, ContextAware {

ContextAwareBase cab = new ContextAwareBase(this);
Expand Down
Loading

0 comments on commit 49f0638

Please sign in to comment.