简体   繁体   中英

Can log4j's error method print stack trace without string argument?

In regards to Log4j v2, I'd like to call log.error(exceptionTypeObj) and have the stack trace printed to my logfile instead of exceptionTypeObj.toString(). Is that possible?

I'd rather write code like log.error(e) and be done with it.

It's error prone and redundant for me to keep writing something like log.error("error", exceptionTypeObj).

The String parameter is redundant because you are not using it for the correct purpose. An Exception will tell you what error occurred but it won't tell you what you were trying to do when it happened. That is why you should always provide the string. Provide a String that says something like "Error occurred while attempting to update the user profile for user {}", and then provide the user name or id.

Without the String parameter you are forcing your support staff to call engineering to find out what the exception means and what they should do about it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM