字符串格式化输出修复

main
youHong.ai 2022-12-13 16:13:25 +08:00
parent 3a2234cbcc
commit 0dde57349d
3 changed files with 36 additions and 19 deletions

View File

@ -3072,20 +3072,25 @@ public class Util extends weaver.general.Util {
Matcher matcher = compile.matcher(logStr); Matcher matcher = compile.matcher(logStr);
int n = 0; int n = 0;
while (matcher.find()) { while (matcher.find()) {
if (n + 1 > args.length) {
break;
}
logStr = logStr.replaceFirst(pattern, "{" + n++ + "}"); logStr = logStr.replaceFirst(pattern, "{" + n++ + "}");
} }
try {
Object arg = args[args.length - 1]; Object arg = args[args.length - 1];
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].toString())); logStr = logStr.replaceFirst(pattern, String.valueOf(args[i]));
} }
return logStr + "\n" + getErrString((Throwable) arg); return logStr + "\n" + getErrString((Throwable) arg);
} }
int i = 0; } catch (Exception ignore) {
for (Object o : args) { }
pattern = "\\{" + i++ + "}"; for (int i = 0; i < args.length; i++) {
logStr = logStr.replaceFirst(pattern, String.valueOf(o)); pattern = "\\{" + i + "}";
logStr = logStr.replaceFirst(pattern, String.valueOf(args[i]));
} }
return logStr; return logStr;
} catch (Exception e) { } catch (Exception e) {

View File

@ -121,11 +121,11 @@ public class HttpUtils {
String str = sqlBuilder.toString().trim(); String str = sqlBuilder.toString().trim();
String removeSeparator = "&"; String removeSeparator = "&";
if (str.endsWith(removeSeparator)) { if (str.endsWith(removeSeparator)) {
// 如果以分&号结尾,则去除&号 // 如果以分&号结尾,则去除&号
str = str.substring(0, str.length() - 1); str = str.substring(0, str.length() - 1);
} }
if (str.trim().startsWith(removeSeparator)) { if (str.trim().startsWith(removeSeparator)) {
// 如果以&开头,则去除& // 如果以&开头,则去除&
str = str.substring(1); str = str.substring(1);
} }
return str; return str;
@ -991,7 +991,7 @@ public class HttpUtils {
nvps.add(new BasicNameValuePair(entry.getKey(), JSON.toJSONString(entry.getValue()))); nvps.add(new BasicNameValuePair(entry.getKey(), JSON.toJSONString(entry.getValue())));
} }
httpPost.setEntity(new UrlEncodedFormEntity(nvps)); httpPost.setEntity(new UrlEncodedFormEntity(nvps));
// } else if (contentType.toUpperCase().startsWith(HttpArgsType.APPLICATION_JSON.toUpperCase())) { // } else if (contentType.toUpperCase().startsWith(HttpArgsType.APPLICATION_JSON.toUpperCase())) {
} else { } else {
StringEntity stringEntity; StringEntity stringEntity;
if (params.containsKey(JSON_PARAM_KEY)) { if (params.containsKey(JSON_PARAM_KEY)) {
@ -1020,8 +1020,12 @@ public class HttpUtils {
*/ */
private HttpPost uploadFileByInputStream(String url, List<HttpMultipartFile> multipartFileList, private HttpPost uploadFileByInputStream(String url, List<HttpMultipartFile> multipartFileList,
Map<String, Object> params, Map<String, String> headers) { Map<String, Object> params, Map<String, String> headers) {
log.info(Util.logStr("start request : url is [{}]" + log.info(Util.logStr("start request : url is [{}],other param [\n{}\n],heard [\n{}\n]", url, JSONObject.toJSONString(params,
"", url)); SerializerFeature.PrettyFormat,
SerializerFeature.WriteDateUseDateFormat),
JSONObject.toJSONString(headers,
SerializerFeature.PrettyFormat,
SerializerFeature.WriteDateUseDateFormat)));
HttpUtilParamInfo httpUtilParamInfo = new HttpUtilParamInfo(); HttpUtilParamInfo httpUtilParamInfo = new HttpUtilParamInfo();
httpUtilParamInfo.setParams(params); httpUtilParamInfo.setParams(params);
httpUtilParamInfo.setUrl(url); httpUtilParamInfo.setUrl(url);
@ -1069,8 +1073,12 @@ public class HttpUtils {
*/ */
private HttpPut uploadFileByInputStreamPut(String url, List<HttpMultipartFile> multipartFileList, private HttpPut uploadFileByInputStreamPut(String url, List<HttpMultipartFile> multipartFileList,
Map<String, Object> params, Map<String, String> headers) { Map<String, Object> params, Map<String, String> headers) {
log.info(Util.logStr("start request : url is [{}]" + log.info(Util.logStr("start request : url is [{}],other param [\n{}\n],heard [\n{}\n]", url, JSONObject.toJSONString(params,
"", url)); SerializerFeature.PrettyFormat,
SerializerFeature.WriteDateUseDateFormat),
JSONObject.toJSONString(headers,
SerializerFeature.PrettyFormat,
SerializerFeature.WriteDateUseDateFormat)));
HttpUtilParamInfo httpUtilParamInfo = new HttpUtilParamInfo(); HttpUtilParamInfo httpUtilParamInfo = new HttpUtilParamInfo();
httpUtilParamInfo.setParams(params); httpUtilParamInfo.setParams(params);
httpUtilParamInfo.setUrl(url); httpUtilParamInfo.setUrl(url);

View File

@ -110,8 +110,8 @@ public class TestOrganization extends BaseTest {
public void testStaticLog() { public void testStaticLog() {
log.info("哈哈哈好的方式"); log.info("哈哈哈好的方式");
String testStr = "weaver.xuanran.wang.schroeder.cus_field_value.PushSealTaskSealValue?sealSnField=sealSn&sealNumField=sealNum&sealSnCusSql=`select\n" + String testStr = "weaver.xuanran.wang.schroeder.cus_field_value.PushSealTaskSealValue?sealSnField=sealSn&sealNumField=sealNum&sealSnCusSql=`select\n" +
"? from jkfdjsfk where id\n" + " ? from jkfdjsfk where id\n" +
"=1`&sealNumCusSql=`select case ? when 0 then htzyzcs when 1 then gzcs else frzcs end from formtable_main_22_dt1 where id = {?dt.id}`&cus=`select * from table id = ? and test = ?`"; " =1`&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&cus=`select? * fr$%&#@!)(<>?/\\{}「」【【】[]~、asfom table where id = '' and teset = #{name}`&niua=卧槽";
Map<String, String> stringStringMap = Util.parseCusInterfacePathParam(testStr); Map<String, String> stringStringMap = Util.parseCusInterfacePathParam(testStr);
System.out.println(stringStringMap); System.out.println(stringStringMap);
System.out.println(JSON.toJSONString(stringStringMap)); System.out.println(JSON.toJSONString(stringStringMap));
@ -140,7 +140,11 @@ public class TestOrganization extends BaseTest {
if (paramValue != null && paramValue.startsWith("`") && paramValue.endsWith("`")) { if (paramValue != null && paramValue.startsWith("`") && paramValue.endsWith("`")) {
} }
} }
}
@Test
public void testLogStr() {
System.out.println(Util.logStr("ajsdf {} jasjdf {}", "asf", "asdfeag"));
} }
@Test @Test