如何使用 java 的 Logger 类记录堆栈跟踪

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31415147/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 11:05:43  来源:igfitidea点击:

How do I log a stacktrace using java's Logger class

javaloggingstack-tracejava.util.logging

提问by Surya Joseph

I am using Java's Loggerclass. I want to pass ex.printStackTrace()into Logger.log(loglevel, String), but printStackTrace()returns void. So I am not able to pass and print the stack trace of the exception.

我正在使用 Java 的Logger课程。我想通过ex.printStackTrace()进入Logger.log(loglevel, String),但printStackTrace()回报率void。所以我无法传递和打印异常的堆栈跟踪。

Is there any way that I can convert voidinto String, or are there any other methods to print the whole stack trace of exceptions?

有什么方法可以转换voidString,或者是否有任何其他方法可以打印异常的整个堆栈跟踪?

采纳答案by Saif Asif

You need to understand that voidis actually nothingness. You cannot convert what is nothing. You might end up printing voidas a string, but (trust me), you don't want that.

你需要明白这void实际上是nothingness. 你不能转换什么都不是。您最终可能会打印void为字符串,但是(相信我),您不希望那样。

I think what you are looking for is

我想你正在寻找的是

// assuming ex is your Exception object
logger.error(ex.getMessage(), ex);
// OR
Logger.log(errorLogLevel, ex.getMessage(), ex)

This will print the error message using the logger that you have configured. For more details, you can take a look at the java docs for Exception#getMessage()

这将使用您配置的记录器打印错误消息。有关更多详细信息,您可以查看Exception#getMessage()的 java 文档

回答by Makoto

You can't convert voidinto String; no such conversion exists. voiddoesn't return anythingback, so you have no value to retrieve.

你不能转换voidString; 不存在这种转换。 void不会返回任何东西,因此您没有任何价值可检索。

What you probably want to do is get the message of the exception instead via ex.getMessage().

您可能想要做的是通过ex.getMessage().

回答by kotakotakota

As Makoto says, you probably want to do an ex.getMessage().

正如 Makoto 所说,你可能想做一个 ex.getMessage()。

To further clarify, voidmeans that there is nothing returned. You can't cast nothing into something :)

进一步澄清,void意味着没有任何返回。你不能把任何东西都投进去:)

回答by Chthonic Project

You can use the getStackTrace()method to get an array of StackTraceElements, and generate a String from there. Otherwise, if just the final error message is sufficient, use the getMessage()method as suggested by Makoto.

您可以使用getStackTrace()方法获取 StackTraceElements 数组,并从那里生成一个字符串。否则,如果只有最终的错误消息就足够了,请使用getMessage()Makoto 建议的方法。

To get the stack trace as a Stringfrom an array of StackTraceElementobjects, you need to iterate over the array (taken from JDK7 source):

要从对象String数组中获取堆栈跟踪StackTraceElement,您需要遍历该数组(取自 JDK7 源代码):

StringBuilder builder = new StringBuilder();
StackTraceElement[] trace = getOurStackTrace();
    for (StackTraceElement traceElement : trace)
        builder.append("\tat " + traceElement + "\n");

Another option is to use printStackTrace(PrintStream s), where you get to specify where you want the stacktrace to be printed:

另一种选择是使用printStackTrace(PrintStream s),您可以在其中指定要打印堆栈跟踪的位置:

ByteArrayOutputStream out1 = new ByteArrayOutputStream();
PrintStream out2 = new PrintStream(out1);
ex.printStackTrace(out2);
String message = out1.toString("UTF8");

回答by hzpz

Use java.util.logging.Logger#log(Level, String, Throwable)and pass in exas third argument like this:

使用java.util.logging.Logger#log(Level, String, Throwable)ex作为第三个参数传入,如下所示:

LOGGER.log(Level.INFO, ex.getMessage(), ex);

回答by Amit.rk3

you CAN convert stacktrace into String using below. If eis the exception object

您可以使用下面的方法将堆栈跟踪转换为字符串。如果e是异常对象

StringWriter stringWriter= new StringWriter();
PrintWriter printWriter= new PrintWriter(stringWriter);
e.printStackTrace(printWriter);
String stackTraceAsString= stringWriter.toString(); 

回答by sleeloy

There's an overloaded printStackTrace method that takes in a PrintWriter.

有一个重载的printStackTrace 方法接收PrintWriter。

You can do something like this

你可以做这样的事情

Writer buffer = new StringWriter();
PrintWriter pw = new PrintWriter(buffer);
ex.printStackTrace(pw);
Logger.log(loglevel, buffer.toString());

回答by Surya Joseph

Thank you all. I am able to log the stack trace details using

谢谢你们。我能够使用记录堆栈跟踪详细信息

LOGGER.log(Level.INFO, ex.getMessage(),ex);
//ex is my exception object

回答by Rutuja

Also another alternative would be:

另一种选择是:

import org.apache.commons.lang3.exception.ExceptionUtils;

log.error("Exception : "+ExceptionUtils.getStackTrace(exception));

回答by iman

With below format you can have the stack trace:

使用以下格式,您可以获得堆栈跟踪:

java.util.logging.SimpleFormatter.format=%1$tF %1$tT [%4$-7s][%2$s] %5$s %6$s%n

The point in this pattern is %6$s. It will print the stack trace.

此模式中的要点是 %6$s。它将打印堆栈跟踪。