Java Exception打印及输出到日志

简介: 有时候如果打印出异常的错误,并记录下来,这里记录一下

1、输出日志时会要输出一些错误的详情信息,下面是纯java获取的代码:

public class ExceptionUtil {
   
    public static String getExceptionInfo(Exception exception) {
   
        StringBuilder msg = new StringBuilder();
        if(exception != null) {
   
            //获取异常的名称
            msg.append(exception.getClass().getName());
            msg.append(" ");
            //异常信息
            msg.append(exception.getMessage());
            msg.append("\n");
            //具体的堆栈
            StackTraceElement[] msgST = exception.getStackTrace();
            for (StackTraceElement temp : msgST) {
   
                msg.append(temp.toString());
                msg.append("\n");
            }
        }
        return msg.toString();
    }
}

2、如果使用日志的话,就更简单:
这里使用的是:org.slf4j.Logger
logger.error("error msg ",e);
上面与e.printStackTrace();等同
注意:如果是logger.error("error msg {} ",e);无法输出堆栈信息,因为转成String了。

相关实践学习
日志服务之使用Nginx模式采集日志
本文介绍如何通过日志服务控制台创建Nginx模式的Logtail配置快速采集Nginx日志并进行多维度分析。
相关文章
|
7月前
|
JavaScript 前端开发 Java
Java的checked exception有意义吗?
Java的checked exception有意义吗?
44 0
|
2月前
Exception in thread “main“ java.lang.NoClassDefFoundError: freemarker/template/Configuration
Exception in thread “main“ java.lang.NoClassDefFoundError: freemarker/template/Configuration
21 0
|
4月前
|
XML Java Maven
nested exception is java.io.FileNotFoundException: class path resource [springmvc.xml] cannot be ope
nested exception is java.io.FileNotFoundException: class path resource [springmvc.xml] cannot be ope
62 0
nested exception is java.io.FileNotFoundException: class path resource [springmvc.xml] cannot be ope
|
5月前
|
Java API
Java Exception 详解
Java Exception 详解
56 1
|
2月前
|
Java Maven Spring
SpringBoot运行出现 Lookup method resolution failed; nested exception is java.lang.IllegalStateException
SpringBoot运行出现 Lookup method resolution failed; nested exception is java.lang.IllegalStateException
66 0
|
2月前
|
Java Spring
上传文件出现 aximum upload size exceeded; nested exception is java.lang.IllegalStateException: org.apache.
上传文件出现 aximum upload size exceeded; nested exception is java.lang.IllegalStateException: org.apache.
12 0
|
2月前
Exception in thread “main“ java.lang.UnsupportedOperationException
Exception in thread “main“ java.lang.UnsupportedOperationException
18 1
|
7月前
|
Java 应用服务中间件
线上临时文件夹报错Failed to parse multipart servlet request; nested exception is java.lang.RuntimeException: java.nio.file.NoSuchFileException
Failed to parse multipart servlet request; nested exception is java.lang.RuntimeException: java.nio.file.NoSuchFileException、tmp、配置文件指定目录
127 0
|
7月前
|
Java Apache Maven
【异常解决】Handler dispatch failed;nested exception is java.lang.NoClassDefFoundError: org/apache/common
【异常解决】Handler dispatch failed;nested exception is java.lang.NoClassDefFoundError: org/apache/common
1208 0
|
4月前
|
JSON 关系型数据库 MySQL
报错:Exception in thread "main" java.lang.UnsupportedOperationException
【1月更文挑战第19天】【1月更文挑战第94篇】报错:Exception in thread "main" java.lang.UnsupportedOperationException
43 1