Skip to content

Commit c30b98d

Browse files
authored
Fix replaceParam when the replaced string contains a replacement marker (#782)
1 parent 67ab8b2 commit c30b98d

File tree

3 files changed

+64
-9
lines changed

3 files changed

+64
-9
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Release Notes.
2222
* Fix Gateway 2.0.x plugin not activated for spring-cloud-starter-gateway 2.0.0.RELEASE.
2323
* Support kafka-clients-3.9.x intercept.
2424
* Upgrade kafka-clients version in optional-reporter-plugins to 3.9.1.
25+
* Fix AbstractLogger replaceParam when the replaced string contains a replacement marker.
2526

2627
All issues and pull requests are [here](https://github.com/apache/skywalking/milestone/242?closed=1)
2728

apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/logging/core/AbstractLogger.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import java.util.HashMap;
3333
import java.util.List;
3434
import java.util.Map;
35-
import java.util.regex.Matcher;
3635

3736
/**
3837
* An abstract class to simplify the real implementation of the loggers.
@@ -189,18 +188,17 @@ protected String replaceParam(String message, Object... parameters) {
189188
int startSize = 0;
190189
int parametersIndex = 0;
191190
int index;
192-
String tmpMessage = message;
193-
while ((index = message.indexOf("{}", startSize)) != -1) {
191+
StringBuilder sb = new StringBuilder(message);
192+
while ((index = sb.indexOf("{}", startSize)) != -1) {
194193
if (parametersIndex >= parameters.length) {
195194
break;
196195
}
197-
/**
198-
* @Fix the Illegal group reference issue
199-
*/
200-
tmpMessage = tmpMessage.replaceFirst("\\{\\}", Matcher.quoteReplacement(String.valueOf(parameters[parametersIndex++])));
201-
startSize = index + 2;
196+
197+
String replaced = String.valueOf(parameters[parametersIndex++]);
198+
sb.replace(index, index + 2, replaced);
199+
startSize = index + replaced.length();
202200
}
203-
return tmpMessage;
201+
return sb.toString();
204202
}
205203

206204
protected void logger(LogLevel level, String message, Throwable e) {
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
19+
package org.apache.skywalking.apm.agent.core.logging.core;
20+
21+
import org.junit.Test;
22+
23+
import static org.junit.Assert.assertEquals;
24+
25+
public class AbstractLoggerTest {
26+
27+
@Test
28+
public void replaceParamNormal() {
29+
TestLogger logger = new TestLogger("AbstractLoggerTest");
30+
String result = logger.testReplaceParam("from {} to {}", "a", "b");
31+
assertEquals("from a to b", result);
32+
}
33+
34+
@Test
35+
public void replaceParamWithReplaceMark() {
36+
TestLogger logger = new TestLogger("AbstractLoggerTest");
37+
String result = logger.testReplaceParam("from {} to {}", "a={}", "b={}");
38+
assertEquals("from a={} to b={}", result);
39+
}
40+
41+
private static class TestLogger extends AbstractLogger {
42+
43+
public TestLogger(String targetClass) {
44+
super(targetClass);
45+
}
46+
47+
@Override
48+
protected String format(LogLevel level, String message, Throwable e) {
49+
return message;
50+
}
51+
52+
String testReplaceParam(String message, Object... parameters) {
53+
return replaceParam(message, parameters);
54+
}
55+
}
56+
}

0 commit comments

Comments
 (0)