fix Illegal group reference

This commit is contained in:
clevertension 2017-09-29 14:48:47 +08:00 committed by IluckySi
parent 2647a906e3
commit 8244256a96
2 changed files with 34 additions and 2 deletions

View File

@ -4,6 +4,8 @@ import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import org.skywalking.apm.agent.core.conf.Config;
import org.skywalking.apm.agent.core.conf.Constants;
import org.skywalking.apm.logging.ILog;
@ -35,8 +37,10 @@ public class EasyLogger implements ILog {
if (parametersIndex >= parameters.length) {
break;
}
tmpMessage = tmpMessage.replaceFirst("\\{\\}", String.valueOf(parameters[parametersIndex++]));
/**
* @Fix the Illegal group reference issue
*/
tmpMessage = tmpMessage.replaceFirst("\\{\\}", Matcher.quoteReplacement(String.valueOf(parameters[parametersIndex++])));
startSize = index + 2;
}
return tmpMessage;

View File

@ -53,6 +53,34 @@ public class EasyLoggerTest {
.println(anyString());
}
@Test
public void testLogWithSpecialChar() {
PrintStream output = Mockito.mock(PrintStream.class);
System.setOut(output);
PrintStream err = Mockito.mock(PrintStream.class);
System.setErr(err);
EasyLogger logger = new EasyLogger(EasyLoggerTest.class);
Assert.assertTrue(logger.isDebugEnable());
Assert.assertTrue(logger.isInfoEnable());
Assert.assertTrue(logger.isWarnEnable());
Assert.assertTrue(logger.isErrorEnable());
logger.debug("$$$$");
logger.debug("hello {}", "$$$$");
logger.info("{}{}");
logger.info("hello {}", "{}{}");
logger.warn("hello {}", "\\");
logger.warn("hello \\");
logger.error("hello <>..");
logger.error("hello ///\\\\", new NullPointerException());
logger.error(new NullPointerException(), "hello {}", "&&&**%%");
Mockito.verify(output, times(9))
.println(anyString());
}
@Test
public void testFormat() {
NullPointerException exception = new NullPointerException();