package com.shanghaichengdi.ghjgitem.util;

import java.io.PrintWriter;
import java.io.StringWriter;
import lombok.extern.slf4j.Slf4j;

/**
 * @ClassName: PrintLogUtils
 * @description: TODO
 * @author: sbj
 * @time: 2023/3/8 14:41
 * @Version: 1.0
 */
@Slf4j
public class PrintLogUtils {

    /**
     * @methodName: printErrorLogInfo
     * @description: TODO 将e.printStackTrace异常添加到创建的StringWriter对象中，并打印到日志文件中
     * @author: sbj
     * @createTime: 2023/3/8 15:06
     * @parames:
     * @return:
     */
    public static void printErrorLogInfo(Exception e){
        StringWriter stringWriter = new StringWriter();
        PrintWriter printWriter = new PrintWriter(stringWriter);
        e.printStackTrace(printWriter);
        log.error(e.getMessage());
        log.error(stringWriter.toString());
    }

    /**
     * @methodName: printErrorLogInfo
     * @description: TODO 将e.printStackTrace异常添加到创建的StringWriter对象中，并打印到日志文件中
     *               支持传一个preMsg，用于拼接到e.getMessage()前
     * @author: sbj
     * @createTime: 2023/3/8 15:08
     * @parames:
     * @return:
     */
    public static void printErrorLogInfo(Exception e, String preMsg){
        StringWriter stringWriter = new StringWriter();
        PrintWriter printWriter = new PrintWriter(stringWriter);
        e.printStackTrace(printWriter);
        log.error(preMsg + e.getMessage());
        log.error(stringWriter.toString());
    }

}
