自定义报警日志打印工具
import org.apache.commons.lang.StringUtils; import java.io.PrintWriter; import java.io.StringWriter; /** * 自定义报警日志打印工具 * * @author Angel挤一挤 * @date 2022/12/13 */ public class MonitorUtil { /** * 处理自定义报警消息 默认Msg长度300 * * @param e * @param customMsg * @return */ public static String alarmMsg(Exception e, String customMsg) { return alarmMsg(e, customMsg, 300); } /** * 重载 处理自定义报警消息 * * @param e * @param customMsg * @param splitLength 切割长度 * @return */ private static String alarmMsg(Exception e, String customMsg, Integer splitLength) { StringBuilder result = new StringBuilder(); if (StringUtils.isNotEmpty(customMsg)) { result.append(customMsg).append(":"); } if (e != null) { StringWriter stringWriter = new StringWriter(); PrintWriter writer = new PrintWriter(stringWriter); e.printStackTrace(writer); StringBuffer buffer = stringWriter.getBuffer(); String errMsg = buffer.toString(); result.append(errMsg); } return splitLength != null && splitLength > 0 ? (result.length() > splitLength ? result.substring(0, splitLength) : result.toString()) : result.toString(); } public static String getErrMsg(Exception e) { StringBuilder result = new StringBuilder(); if (e != null) { StringWriter stringWriter = new StringWriter(); PrintWriter writer = new PrintWriter(stringWriter); e.printStackTrace(writer); StringBuffer buffer = stringWriter.getBuffer(); String errMsg = buffer.toString(); result.append(errMsg); } return result.toString(); } public static String getErrMsg(String customMsg, Exception e) { StringBuilder result = new StringBuilder(); if (StringUtils.isNotBlank(customMsg)) { result.append(customMsg); } if (e != null) { StringWriter stringWriter = new StringWriter(); PrintWriter writer = new PrintWriter(stringWriter); e.printStackTrace(writer); StringBuffer buffer = stringWriter.getBuffer(); String errMsg = buffer.toString(); result.append(errMsg); } return result.toString(); } }
标签:PrintWriter,java,String,MonitorUtil,stringWriter,报错,result,new,customMsg From: https://www.cnblogs.com/sxdcgaq8080/p/18542215