Skip to content

Commit

Permalink
Merge pull request #482 from clevertension/my-main
Browse files Browse the repository at this point in the history
fix Illegal group reference
  • Loading branch information
wu-sheng authored Sep 29, 2017
2 parents ea398f0 + ed46538 commit 1370f3c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
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;
Expand Down Expand Up @@ -35,8 +37,10 @@ private String replaceParam(String message, Object... parameters) {
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,42 @@ public void testLog() {
.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();
EasyLogger logger = new EasyLogger(EasyLoggerTest.class);
String formatLines = logger.format(exception);
String[] lines = formatLines.split(Constants.LINE_SEPARATOR);
Assert.assertEquals("java.lang.NullPointerException", lines[1]);
Assert.assertEquals("\tat org.skywalking.apm.agent.core.logging.EasyLoggerTest.testFormat(EasyLoggerTest.java:58)", lines[2]);
Assert.assertEquals("\tat org.skywalking.apm.agent.core.logging.EasyLoggerTest.testFormat(EasyLoggerTest.java:86)", lines[2]);
}

@AfterClass
Expand Down

0 comments on commit 1370f3c

Please sign in to comment.