如何将堆栈跟踪发送到 log4j?
假设您捕获了一个异常,并在执行 e.printStackTrace() 时在标准输出(例如控制台)上得到以下内容:
java.io.FileNotFoundException: so.txt
at java.io.FileInputStream.<init>(FileInputStream.java)
at ExTest.readMyFile(ExTest.java:19)
at ExTest.main(ExTest.java:7)
现在我想将其发送到记录器,例如,比如说,log4j 得到以下信息:
31947 [AWT-EventQueue-0] ERROR Java.io.FileNotFoundException: so.txt
32204 [AWT-EventQueue-0] ERROR at java.io.FileInputStream.<init>(FileInputStream.java)
32235 [AWT-EventQueue-0] ERROR at ExTest.readMyFile(ExTest.java:19)
32370 [AWT-EventQueue-0] ERROR at ExTest.main(ExTest.java:7)
我该怎么做?
try {
...
} catch (Exception e) {
final String s;
... // <-- What goes here?
log.error( s );
}
Say you catch an exception and get the following on the standard output (like, say, the console) if you do a e.printStackTrace() :
java.io.FileNotFoundException: so.txt
at java.io.FileInputStream.<init>(FileInputStream.java)
at ExTest.readMyFile(ExTest.java:19)
at ExTest.main(ExTest.java:7)
Now I want to send this instead to a logger like, say, log4j to get the following:
31947 [AWT-EventQueue-0] ERROR Java.io.FileNotFoundException: so.txt
32204 [AWT-EventQueue-0] ERROR at java.io.FileInputStream.<init>(FileInputStream.java)
32235 [AWT-EventQueue-0] ERROR at ExTest.readMyFile(ExTest.java:19)
32370 [AWT-EventQueue-0] ERROR at ExTest.main(ExTest.java:7)
How can I do this?
try {
...
} catch (Exception e) {
final String s;
... // <-- What goes here?
log.error( s );
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
您将异常直接传递给记录器,例如
由 log4j 来呈现堆栈跟踪。
You pass the exception directly to the logger, e.g.
It's up to log4j to render the stack trace.
如果您想记录堆栈跟踪而不涉及异常,只需执行以下操作:
If you want to log a stacktrace without involving an exception just do this:
您还可以通过 ExceptionUtils.getStackTrace 获取字符串形式的堆栈跟踪。
请参阅: ExceptionUtils.java
我仅将它用于
log.debug
,以保持log.error
简单。You can also get stack trace as string via
ExceptionUtils.getStackTrace
.See: ExceptionUtils.java
I use it only for
log.debug
, to keeplog.error
simple.skaffman的答案绝对是正确答案。所有记录器方法,例如
error()
、warn()
、info()
、debug()
均采用 Throwable作为第二个参数:但是,您也可以将堆栈跟踪提取为字符串。有时,如果您希望使用“{}”占位符利用格式化功能,它可能会很有用 - 请参阅方法
void info(String var1, Object... var2);
在这种情况下,假设您有一个stacktrace as String,那么你实际上可以这样做:这将打印参数化消息和最后的堆栈跟踪,就像方法一样:
logger.error("error: ", e);
我实际上编写了一个开源库,它有一个实用程序,用于将堆栈跟踪提取为字符串,并提供一个选项来智能地过滤掉堆栈跟踪中的一些噪音。即,如果您指定您对提取的堆栈跟踪感兴趣的包前缀,则会从一些不相关的部分中过滤掉,并留下非常简洁的信息。这是文章的链接,解释了该库有哪些实用程序以及从哪里获取它(作为 Maven 工件和 git 源)以及如何使用它。 具有堆栈跟踪过滤功能的开源 Java 库, Silent String解析Unicode转换器和版本比较参见段落“Stacktrace噪声过滤器”
The answer from skaffman is definitely the correct answer. All logger methods such as
error()
,warn()
,info()
,debug()
take Throwable as a second parameter:However, you can extract stacktrace as a String as well. Sometimes it could be useful if you wish to take advantage of formatting feature using "{}" placeholder - see method
void info(String var1, Object... var2);
In this case say you have a stacktrace as String, then you can actually do something like this:This will print parametrized message and the stacktrace at the end the same way it does for method:
logger.error("error: ", e);
I actually wrote an open source library that has a Utility for extraction of a stacktrace as a String with an option to smartly filter out some noise out of stacktrace. I.e. if you specify the package prefix that you are interested in your extracted stacktrace would be filtered out of some irrelevant parts and leave you with very consized info. Here is the link to the article that explains what utilities the library has and where to get it (both as maven artifacts and git sources) and how to use it as well. Open Source Java library with stack trace filtering, Silent String parsing Unicode converter and Version comparison See the paragraph "Stacktrace noise filter"
只是因为它发生在我身上并且可能有用。
如果这样做,
您将获得异常的标头,而不是整个堆栈跟踪。因为记录器会认为你正在传递一个字符串。
正如 skaffman 所说,不用
{}
即可完成Just because it happened to me and can be useful.
If you do this
you will get the header of the exception and not the whole stacktrace. Because the logger will think that you are passing a String.
Do it without
{}
as skaffman said在 Log4j 2 中,您可以使用 Logger.writing() 用于记录捕获的异常的堆栈跟踪。
In Log4j 2, you can use Logger.catching() to log a stacktrace from an exception that was caught.
这个答案可能与所提出的问题无关,但与问题的标题有关。
或
或
This answer may be not related to the question asked but related to title of the question.
OR
OR
这将是很好的 log4j 错误/异常日志记录 - 可由 splunk/其他日志记录/监控软件读取。一切都是键值对的形式。
log4j 将从 Exception obj
e
获取堆栈跟踪this would be good log4j error/exception logging - readable by splunk/other logging/monitoring s/w. everything is form of key-value pair.
log4j would get the stack trace from Exception obj
e
您可以使用以下代码:
在这里您只需调用:
LogWriterUtility.errorWithAnalysis( YOUR_EXCEPTION_INSTANCE);
它会将 stackTrace 打印到您的日志中。
You can use bellow code:
Here you can just call :
LogWriterUtility.errorWithAnalysis( YOUR_EXCEPTION_INSTANCE);
It will print stackTrace into your log.
创建这个
类
:在代码中调用它
Create this
class
:Call this in your code