日志增加string字符串格式化标签包括转义,截取,null字符处理

This commit is contained in:
zhanghongqing
2018-08-01 10:26:29 +08:00
parent 6bd5be9722
commit 7f05359125
2 changed files with 35 additions and 1 deletions

View File

@@ -472,4 +472,32 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils {
return true; return true;
} }
public static String stringFormat(String str,int len){
if(str.trim().equals("null")||str==null){
return "";
}else{
str = Encodes.escapeHtml(str);
if(len!=-1){
try {
StringBuilder sb = new StringBuilder();
int currentLength = 0;
for (char c : str.toCharArray()) {
currentLength += String.valueOf(c).getBytes("GBK").length;
if (currentLength <= len - 3) {
sb.append(c);
} else {
sb.append("...");
break;
}
}
return sb.toString();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
return str;
}
}
} }

View File

@@ -354,6 +354,12 @@
<example>${fns:getPermissionByNo(str1,list)}</example> <example>${fns:getPermissionByNo(str1,list)}</example>
</function> </function>
<function>
<description>字符串转义及截取,null转""</description>
<name>stringFormat</name>
<function-class>com.nis.util.StringUtils</function-class>
<function-signature>java.lang.String stringFormat(java.lang.String, int)</function-signature>
<example>${fns:stringFormat(str,len)}</example>
</function>
</taglib> </taglib>