Skip to content

Commit d4804f9

Browse files
committed
fix incompatibile change, Spring-Boot issue 885
Signed-off-by: Ceki Gulcu <ceki@qos.ch>
1 parent 3492bc8 commit d4804f9

File tree

6 files changed

+106
-55
lines changed

6 files changed

+106
-55
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Logback: the reliable, generic, fast and flexible logging framework.
3+
* Copyright (C) 1999-2024, QOS.ch. All rights reserved.
4+
*
5+
* This program and the accompanying materials are dual-licensed under
6+
* either the terms of the Eclipse Public License v1.0 as published by
7+
* the Eclipse Foundation
8+
*
9+
* or (per the licensee's choosing)
10+
*
11+
* under the terms of the GNU Lesser General Public License version 2.1
12+
* as published by the Free Software Foundation.
13+
*/
14+
15+
package ch.qos.logback.classic.pattern;
16+
17+
import ch.qos.logback.classic.Level;
18+
import ch.qos.logback.classic.LoggerContext;
19+
import ch.qos.logback.classic.PatternLayout;
20+
import ch.qos.logback.classic.spi.LoggingEvent;
21+
import ch.qos.logback.core.CoreConstants;
22+
import org.junit.jupiter.api.Test;
23+
24+
import java.util.HashMap;
25+
import java.util.Map;
26+
27+
import static org.junit.jupiter.api.Assertions.assertEquals;
28+
import static org.junit.jupiter.api.Assertions.assertNull;
29+
30+
public class LegacyPatternLayoutTest {
31+
32+
LoggerContext context = new LoggerContext();
33+
34+
@Test public void subPattern() {
35+
SubPatternLayout layout = new SubPatternLayout();
36+
layout.setPattern("%"+SubPatternLayout.DOOO);
37+
layout.setContext(context);
38+
layout.start();
39+
LoggingEvent event = new LoggingEvent();
40+
event.setTimeStamp(0);
41+
42+
String result = layout.doLayout(event);
43+
assertEquals("1970-01-01 01:00:00,000", result);
44+
}
45+
46+
@Test
47+
public void fromContext() {
48+
Map<String, String> registry = (Map<String, String>) this.context
49+
.getObject(CoreConstants.PATTERN_RULE_REGISTRY);
50+
//
51+
assertNull(registry);
52+
if(registry == null) {
53+
registry = new HashMap<String, String>();
54+
this.context.putObject(CoreConstants.PATTERN_RULE_REGISTRY, registry);
55+
}
56+
57+
registry.put("legacy", LevelConverter.class.getName());
58+
59+
PatternLayout patternLayout = new PatternLayout();
60+
patternLayout.setPattern("%legacy");
61+
patternLayout.setContext(context);
62+
patternLayout.start();
63+
LoggingEvent event = new LoggingEvent();
64+
event.setLevel(Level.WARN);
65+
String result = patternLayout.doLayout(event);
66+
assertEquals("WARN", result);
67+
}
68+
69+
}

logback-classic/src/test/java/ch/qos/logback/classic/pattern/SubPatternLayoutTest.java

-38
This file was deleted.

logback-core/src/main/java/ch/qos/logback/core/CoreConstants.java

+11
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,23 @@ public class CoreConstants {
5252
* The default context name.
5353
*/
5454
public static final String DEFAULT_CONTEXT_NAME = "default";
55+
56+
/**
57+
* Customized pattern conversion rules are stored under this key in the
58+
* {@link Context} object store.
59+
*
60+
* @since 1.5.14
61+
*/
62+
public static final String PATTERN_RULE_REGISTRY_FOR_SUPPLIERS = "PATTERN_RULE_REGISTRY_FOR_SUPPLIERS";
63+
5564
/**
5665
* Customized pattern conversion rules are stored under this key in the
5766
* {@link Context} object store.
5867
*/
5968
public static final String PATTERN_RULE_REGISTRY = "PATTERN_RULE_REGISTRY";
6069

70+
71+
6172
public static final String ISO8601_STR = "ISO8601";
6273
public static final String ISO8601_PATTERN = "yyyy-MM-dd HH:mm:ss,SSS";
6374

logback-core/src/main/java/ch/qos/logback/core/html/HTMLLayoutBase.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public Map<String, Supplier<DynamicConverter>> getEffectiveConverterMap() {
117117
if (context != null) {
118118
@SuppressWarnings("unchecked")
119119
Map<String, Supplier<DynamicConverter>> contextMap = (Map<String, Supplier<DynamicConverter>>) context
120-
.getObject(CoreConstants.PATTERN_RULE_REGISTRY);
120+
.getObject(CoreConstants.PATTERN_RULE_REGISTRY_FOR_SUPPLIERS);
121121
if (contextMap != null) {
122122
effectiveMap.putAll(contextMap);
123123
}

logback-core/src/main/java/ch/qos/logback/core/model/processor/ConversionRuleModelHandler.java

+2-4
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@
2626
import java.util.Map;
2727
import java.util.function.Supplier;
2828

29-
import static ch.qos.logback.core.joran.JoranConstants.CONVERSION_WORD_ATTRIBUTE;
30-
3129
public class ConversionRuleModelHandler extends ModelHandlerBase {
3230

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

6058
try {
6159
Map<String, Supplier<DynamicConverter>> ruleRegistry = (Map<String, Supplier<DynamicConverter>>) context
62-
.getObject(CoreConstants.PATTERN_RULE_REGISTRY);
60+
.getObject(CoreConstants.PATTERN_RULE_REGISTRY_FOR_SUPPLIERS);
6361
if (ruleRegistry == null) {
6462
ruleRegistry = new HashMap<>();
65-
context.putObject(CoreConstants.PATTERN_RULE_REGISTRY, ruleRegistry);
63+
context.putObject(CoreConstants.PATTERN_RULE_REGISTRY_FOR_SUPPLIERS, ruleRegistry);
6664
}
6765
// put the new rule into the rule registry
6866
addInfo("registering conversion word " + conversionWord + " with class [" + converterClass + "]");

logback-core/src/main/java/ch/qos/logback/core/pattern/PatternLayoutBase.java

+23-12
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,14 @@ public Map<String, Supplier<DynamicConverter>> getEffectiveConverterMap() {
8181
effectiveMap.putAll(defaultConverterSupplierMap);
8282
}
8383

84-
caterForLegacy_DefaultConverterMap(effectiveMap);
84+
caterForLegacyConverterMaps(effectiveMap);
8585

8686
// contextMap is more specific than the default map
8787
Context context = getContext();
8888
if (context != null) {
8989
@SuppressWarnings("unchecked")
9090
Map<String, Supplier<DynamicConverter>> contextMap = (Map<String, Supplier<DynamicConverter>>) context
91-
.getObject(CoreConstants.PATTERN_RULE_REGISTRY);
91+
.getObject(CoreConstants.PATTERN_RULE_REGISTRY_FOR_SUPPLIERS);
9292
if (contextMap != null) {
9393
effectiveMap.putAll(contextMap);
9494
}
@@ -104,18 +104,29 @@ public Map<String, Supplier<DynamicConverter>> getEffectiveConverterMap() {
104104
*
105105
* @param effectiveMap
106106
*/
107-
private void caterForLegacy_DefaultConverterMap(Map<String, Supplier<DynamicConverter>> effectiveMap) {
108-
// this transformation is for backward compatibility of existing code
107+
private void caterForLegacyConverterMaps(Map<String, Supplier<DynamicConverter>> effectiveMap) {
108+
Map<String, String> mapFromContext = (Map<String, String>) this.context
109+
.getObject(CoreConstants.PATTERN_RULE_REGISTRY);
110+
111+
migrateFromStringMapToSupplierMap(mapFromContext, effectiveMap);
112+
109113
Map<String, String> defaultConverterMap = getDefaultConverterMap();
110-
if(defaultConverterMap != null) {
111-
for(Map.Entry<String, String> entry: defaultConverterMap.entrySet()) {
112-
String key = entry.getKey().toString();
113-
String converterClassName = entry.getValue();
114-
ConverterSupplierByClassName converterSupplierByClassName = new ConverterSupplierByClassName(key, converterClassName);
115-
converterSupplierByClassName.setContext(getContext());
116-
effectiveMap.put(key, converterSupplierByClassName);
117-
}
114+
migrateFromStringMapToSupplierMap(defaultConverterMap, effectiveMap);
115+
}
116+
117+
private void migrateFromStringMapToSupplierMap(Map<String, String> legacyMap, Map<String, Supplier<DynamicConverter>> targetSupplierMap) {
118+
if(legacyMap == null)
119+
return;
120+
121+
// this transformation is for backward compatibility of existing code
122+
for(Map.Entry<String, String> entry: legacyMap.entrySet()) {
123+
String key = entry.getKey();
124+
String converterClassName = entry.getValue();
125+
ConverterSupplierByClassName converterSupplierByClassName = new ConverterSupplierByClassName(key, converterClassName);
126+
converterSupplierByClassName.setContext(getContext());
127+
targetSupplierMap.put(key, converterSupplierByClassName);
118128
}
129+
119130
}
120131

121132
public void start() {

0 commit comments

Comments
 (0)