遇到一个新需求:Log4j日志邮件,异常栈信息显示太多,只需要显示前10行
为此,我重写了DefineLayOut类,基本是拷贝HTMLLayout类,只修改了一个方法,实现如上功能
void appendThrowableAsHTML(String[] s, StringBuffer sbuf) {
if (s != null) {
int len = s.length;
if (len == 0)
return;
sbuf.append(Transform.escapeTags(s[0]));
sbuf.append(Layout.LINE_SEP);
for (int i = 1; i < 10; i++) {
sbuf.append(TRACE_PREFIX);
sbuf.append(Transform.escapeTags(s[i]));
sbuf.append(Layout.LINE_SEP);
}
}
}
这里不循环便利所有数组中的数据,只遍历9条数据。