字符串格式化输出修复,替换$引发的Illegal group reference错误

main
youHong.ai 2022-12-13 16:55:52 +08:00
parent 0dde57349d
commit 8b55a4bd64
2 changed files with 4 additions and 3 deletions

View File

@ -3082,7 +3082,7 @@ public class Util extends weaver.general.Util {
if (arg instanceof Throwable) { if (arg instanceof Throwable) {
for (int i = 0; i < args.length - 1; i++) { for (int i = 0; i < args.length - 1; i++) {
pattern = "\\{" + i + "}"; pattern = "\\{" + i + "}";
logStr = logStr.replaceFirst(pattern, String.valueOf(args[i])); logStr = logStr.replaceFirst(pattern, Matcher.quoteReplacement(String.valueOf(args[i])));
} }
return logStr + "\n" + getErrString((Throwable) arg); return logStr + "\n" + getErrString((Throwable) arg);
} }
@ -3090,7 +3090,7 @@ public class Util extends weaver.general.Util {
} }
for (int i = 0; i < args.length; i++) { for (int i = 0; i < args.length; i++) {
pattern = "\\{" + i + "}"; pattern = "\\{" + i + "}";
logStr = logStr.replaceFirst(pattern, String.valueOf(args[i])); logStr = logStr.replaceFirst(pattern, Matcher.quoteReplacement(String.valueOf(args[i])));
} }
return logStr; return logStr;
} catch (Exception e) { } catch (Exception e) {

View File

@ -144,7 +144,8 @@ public class TestOrganization extends BaseTest {
@Test @Test
public void testLogStr() { public void testLogStr() {
System.out.println(Util.logStr("ajsdf {} jasjdf {}", "asf", "asdfeag")); System.out.println(Util.logStr("ajsdf {} jasjdf {}", "{\"cus\":\"select? * fr$%&#@!)(<>?/\\\\「」【【】[]~、asfom table where id = '' and teset = #{name}\",\"sealSnCusSql\":\"select \\n? from jkfdjsfk where id \\n=1\",\"niua\":\"卧槽\",\"sealSnField\":\"sealSn\",\"sealNumField\":\"sealNum\",\"sealNumCusSql\":\"select case ? when 0 then htzyzcs when 1 then gzcs else frzcs end from formtable_main_22_dt1 where id = {?dt.id}\",\"hah\":\"liuliu\"} "));
System.out.println("{0} {} ".replaceFirst("\\{0}", Matcher.quoteReplacement("{\"cus\":\"select? * fr$%&#@!)(<>?/\\\\「」【【】[]~、asfom table where id = '' and teset = #{name}\",\"sealSnCusSql\":\"select \\n? from jkfdjsfk where id \\n=1\",\"niua\":\"卧槽\",\"sealSnField\":\"sealSn\",\"sealNumField\":\"sealNum\",\"sealNumCusSql\":\"select case ? when 0 then htzyzcs when 1 then gzcs else frzcs end from formtable_main_22_dt1 where id = {?dt.id}\",\"hah\":\"liuliu\"} ")));
} }
@Test @Test