slf4j How to print Java exception stack information Throwable object

Source: Internet
Author: User
Tags stack trace throwable

SLF4J 1.6.0 Previous version, if you print the exception stack information, you must use the

Log.error (String msg, throwable t)

Log.info and other corresponding methods.

If MSG contains variables, MSG is generally formatted with the String.Format method.


If you use


error
(String format, Object... arguments)
Other methods, the exception stack information is lost.

Fortunately,slf4j 1.6.0 later versions of this unfriendly exception information log improved.


Error (String format, object... arguments) This method also prints the exception stack information, but specifies that the Throwable object must be

The last parameter. If you do not follow this rule, the exception stack information is not log out.


Official 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 a Exception at the ERROR level, Y OU must add an accompanying message. For example,

logger   error   (  "some Accompanying message "   e  Span class= "pun" style= "Color:rgb (102,102,0)");   

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

While These is valid arguments, there is 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 have access to more contextual information than at th E 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 C An is 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 a accompanying message, it would be the job of the logging System to invent a message. This was actually what the throwing (string Sourceclass, String Sourcemethod, Throwable thrown) method in Java.util.logging p Ackage does. (It decides on its own, accompanying message is the string "THROW".)

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


In the presence of an exception/throwable, are 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 a exception, assuming the exception is the last parameter. Thus,

Strings= "Hello World";Try {  IntegerI= Integer.valueOf(s);} Catch (NumberFormatExceptione) {Logger.Error("Failed to format {}",s,e);}

will Print The numberformatexception  with its stack trace As expected. The Java compiler would invoke The error method taking a String and Object arguments. SLF4J, in accordance with the programmer's most probable intention, would 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 isn't the last argument, it'll be treated as a plain object and its stack trace won't be printed. However, such situations should not occur in practice.


slf4j How to print Java exception stack information Throwable object

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.