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等
网友评论