Skip to content

Commit

Permalink
fix incompatibile change, Spring-Boot issue 885
Browse files Browse the repository at this point in the history
Signed-off-by: Ceki Gulcu <[email protected]>
  • Loading branch information
ceki committed Dec 19, 2024
1 parent 3492bc8 commit d4804f9
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 55 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* 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.Level;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.PatternLayout;
import ch.qos.logback.classic.spi.LoggingEvent;
import ch.qos.logback.core.CoreConstants;
import org.junit.jupiter.api.Test;

import java.util.HashMap;
import java.util.Map;

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

public class LegacyPatternLayoutTest {

LoggerContext context = new LoggerContext();

@Test public void subPattern() {
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);
}

@Test
public void fromContext() {
Map<String, String> registry = (Map<String, String>) this.context
.getObject(CoreConstants.PATTERN_RULE_REGISTRY);
//
assertNull(registry);
if(registry == null) {
registry = new HashMap<String, String>();
this.context.putObject(CoreConstants.PATTERN_RULE_REGISTRY, registry);
}

registry.put("legacy", LevelConverter.class.getName());

PatternLayout patternLayout = new PatternLayout();
patternLayout.setPattern("%legacy");
patternLayout.setContext(context);
patternLayout.start();
LoggingEvent event = new LoggingEvent();
event.setLevel(Level.WARN);
String result = patternLayout.doLayout(event);
assertEquals("WARN", result);
}

}

This file was deleted.

11 changes: 11 additions & 0 deletions logback-core/src/main/java/ch/qos/logback/core/CoreConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,23 @@ public class CoreConstants {
* The default context name.
*/
public static final String DEFAULT_CONTEXT_NAME = "default";

/**
* Customized pattern conversion rules are stored under this key in the
* {@link Context} object store.
*
* @since 1.5.14
*/
public static final String PATTERN_RULE_REGISTRY_FOR_SUPPLIERS = "PATTERN_RULE_REGISTRY_FOR_SUPPLIERS";

/**
* Customized pattern conversion rules are stored under this key in the
* {@link Context} object store.
*/
public static final String PATTERN_RULE_REGISTRY = "PATTERN_RULE_REGISTRY";



public static final String ISO8601_STR = "ISO8601";
public static final String ISO8601_PATTERN = "yyyy-MM-dd HH:mm:ss,SSS";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public Map<String, Supplier<DynamicConverter>> getEffectiveConverterMap() {
if (context != null) {
@SuppressWarnings("unchecked")
Map<String, Supplier<DynamicConverter>> contextMap = (Map<String, Supplier<DynamicConverter>>) context
.getObject(CoreConstants.PATTERN_RULE_REGISTRY);
.getObject(CoreConstants.PATTERN_RULE_REGISTRY_FOR_SUPPLIERS);
if (contextMap != null) {
effectiveMap.putAll(contextMap);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
import java.util.Map;
import java.util.function.Supplier;

import static ch.qos.logback.core.joran.JoranConstants.CONVERSION_WORD_ATTRIBUTE;

public class ConversionRuleModelHandler extends ModelHandlerBase {

private boolean inError;
Expand Down Expand Up @@ -59,10 +57,10 @@ public void handle(ModelInterpretationContext mic, Model model) throws ModelHand

try {
Map<String, Supplier<DynamicConverter>> ruleRegistry = (Map<String, Supplier<DynamicConverter>>) context
.getObject(CoreConstants.PATTERN_RULE_REGISTRY);
.getObject(CoreConstants.PATTERN_RULE_REGISTRY_FOR_SUPPLIERS);
if (ruleRegistry == null) {
ruleRegistry = new HashMap<>();
context.putObject(CoreConstants.PATTERN_RULE_REGISTRY, ruleRegistry);
context.putObject(CoreConstants.PATTERN_RULE_REGISTRY_FOR_SUPPLIERS, ruleRegistry);
}
// put the new rule into the rule registry
addInfo("registering conversion word " + conversionWord + " with class [" + converterClass + "]");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,14 @@ public Map<String, Supplier<DynamicConverter>> getEffectiveConverterMap() {
effectiveMap.putAll(defaultConverterSupplierMap);
}

caterForLegacy_DefaultConverterMap(effectiveMap);
caterForLegacyConverterMaps(effectiveMap);

// contextMap is more specific than the default map
Context context = getContext();
if (context != null) {
@SuppressWarnings("unchecked")
Map<String, Supplier<DynamicConverter>> contextMap = (Map<String, Supplier<DynamicConverter>>) context
.getObject(CoreConstants.PATTERN_RULE_REGISTRY);
.getObject(CoreConstants.PATTERN_RULE_REGISTRY_FOR_SUPPLIERS);
if (contextMap != null) {
effectiveMap.putAll(contextMap);
}
Expand All @@ -104,18 +104,29 @@ public Map<String, Supplier<DynamicConverter>> getEffectiveConverterMap() {
*
* @param effectiveMap
*/
private void caterForLegacy_DefaultConverterMap(Map<String, Supplier<DynamicConverter>> effectiveMap) {
// this transformation is for backward compatibility of existing code
private void caterForLegacyConverterMaps(Map<String, Supplier<DynamicConverter>> effectiveMap) {
Map<String, String> mapFromContext = (Map<String, String>) this.context
.getObject(CoreConstants.PATTERN_RULE_REGISTRY);

migrateFromStringMapToSupplierMap(mapFromContext, effectiveMap);

Map<String, String> defaultConverterMap = getDefaultConverterMap();
if(defaultConverterMap != null) {
for(Map.Entry<String, String> entry: defaultConverterMap.entrySet()) {
String key = entry.getKey().toString();
String converterClassName = entry.getValue();
ConverterSupplierByClassName converterSupplierByClassName = new ConverterSupplierByClassName(key, converterClassName);
converterSupplierByClassName.setContext(getContext());
effectiveMap.put(key, converterSupplierByClassName);
}
migrateFromStringMapToSupplierMap(defaultConverterMap, effectiveMap);
}

private void migrateFromStringMapToSupplierMap(Map<String, String> legacyMap, Map<String, Supplier<DynamicConverter>> targetSupplierMap) {
if(legacyMap == null)
return;

// this transformation is for backward compatibility of existing code
for(Map.Entry<String, String> entry: legacyMap.entrySet()) {
String key = entry.getKey();
String converterClassName = entry.getValue();
ConverterSupplierByClassName converterSupplierByClassName = new ConverterSupplierByClassName(key, converterClassName);
converterSupplierByClassName.setContext(getContext());
targetSupplierMap.put(key, converterSupplierByClassName);
}

}

public void start() {
Expand Down

0 comments on commit d4804f9

Please sign in to comment.