码迷,mamicode.com
首页 > 编程语言 > 详细

slf4j如何打印java异常堆栈信息throwable对象

时间:2015-02-06 21:54:47      阅读:2922      评论:0      收藏:0      [点我收藏+]

标签:slf4j如何打印java异常堆栈信息t

SLF4J 1.6.0 以前的版本,如果打印异常堆栈信息,必须用

log.error(String msg, Throwable t)

log.info等对应方法.

如果msg含有变量,一般用String.format方法格式化msg.


如果用


error
(String format, Object... arguments)
等其它方法,异常堆栈信息会丢失.

幸好,SLF4J 1.6.0以后的版本对这个不友好的异常信息log 改进了.


error(String format, Object... arguments) 这个方法也会打印异常堆栈信息,只不过规定throwable对象必须为

最后一个参数.如果不遵守这个规定,异常堆栈信息不会log出来.


官方FAQ:http://www.slf4j.org/faq.html

Can I log an exception without an accompanying message?

In short, no.

If e is an Exception, and you would like to log an exception at the ERROR level, you must add an accompanying message. For example,

logger.error("some accompanying message", e);

You might legitimately argue that not all exceptions have a meaningful message to accompany them. Moreover, a good exception should already contain a self explanatory description. The accompanying message may therefore be considered redundant.

While these are valid arguments, there are three opposing arguments also worth considering. First, on many, albeit not all occasions, the accompanying message can convey useful information nicely complementing the description contained in the exception. Frequently, at the point where the exception is logged, the developer has access to more contextual information than at the point where the exception is thrown. Second, it is not difficult to imagine more or less generic messages, e.g. "Exception caught", "Exception follows", that can be used as the first argument for error(String msg, Throwable t) invocations. Third, most log output formats display the message on a line, followed by the exception on a separate line. Thus, the message line would look inconsistent without a message.

In short, if the user were allowed to log an exception without an accompanying message, it would be the job of the logging system to invent a message. This is actually what the throwing(String sourceClass, String sourceMethod, Throwable thrown)method in java.util.logging package does. (It decides on its own that accompanying message is the string "THROW".)

It may initially appear strange to require an accompanying message to log an exception. Nevertheless, this is common practice in all log4j derived systems such as java.util.logging, logkit, etc. and of course log4j itself. It seems that the current consensus considers requiring an accompanying message as a good a thing (TM).


In the presence of an exception/throwable, is it possible to parameterize a logging statement?

Yes, as of SLF4J 1.6.0, but not in previous versions. The SLF4J API supports parametrization in the presence of an exception, assuming the exception is the last parameter. Thus,

String s = "Hello world";
try {
  Integer i = Integer.valueOf(s);
} catch (NumberFormatException e) {
  logger.error("Failed to format {}", s, e);
}

will print the NumberFormatException with its stack trace as expected. The java compiler will invoke the error method taking a String and two Object arguments. SLF4J, in accordance with the programmer‘s most probable intention, will interpret NumberFormatException instance as a throwable instead of an unused Object parameter. In SLF4J versions prior to 1.6.0, the NumberFormatException instance was simply ignored.

If the exception is not the last argument, it will be treated as a plain object and its stack trace will NOT be printed. However, such situations should not occur in practice.


slf4j如何打印java异常堆栈信息throwable对象

标签:slf4j如何打印java异常堆栈信息t

原文地址:http://blog.csdn.net/doctor_who2004/article/details/43569407

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!