改造java异常捕捉在日志中输出

 1.方法:

    public static String getTrace(Throwable t) { 

       StringWriter stringWriter= new StringWriter();
       PrintWriter writer= new PrintWriter(stringWriter);
       t.printStackTrace(writer);
       StringBuffer buffer= stringWriter.getBuffer();
       return buffer.toString();
   }

2.调用方法:
    try(){

        }catch(IoException e)
            e.printStackTrace();
            logger.error(getTrace(e)); //将异常输出到文件

            // logger.error(getTrace(e.fillInStackTrace()));
      } 

 

学习来源:

       https://blog.csdn.net/xuanjiewu/article/details/79283178

       http://blog.csdn.net/mayuanxiaonong/article/details/23947247

       http://blog.csdn.net/menghuannvxia/article/details/52400365

posted @ 2020-10-21 10:42  小窝蜗  阅读(262)  评论(0编辑  收藏  举报