2021-09-17 IO流(异常的日志信息)

2021-09-27  本文已影响0人  Denholm
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.text.SimpleDateFormat;
import java.util.Date;

public class ExceptionInfo {

    public static void main(String[] args) {
        try {
            int[] arr = new int[5];
            System.out.println(arr[8]);
        } catch (Exception e) {
            try {
                Date date = new Date();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
                String format = sdf.format(date);
                PrintStream ps = new PrintStream(new FileOutputStream("E:\\exception.log"));
                ps.print(format);
                e.printStackTrace(ps);
            } catch (IOException e2) {
                throw new RuntimeException("日志文件创建失败");
            }
            e.printStackTrace(System.out);
        }
    }
    
}

常见的日志框架:log4j、slf4j等

上一篇 下一篇

猜你喜欢

热点阅读