Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
417 views
in Technique[技术] by (71.8m points)

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

I am using Java's Logger class. 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.

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

question from:https://stackoverflow.com/questions/31415147/how-do-i-log-a-stacktrace-using-javas-logger-class

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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()


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...