How to format the error log with a variable and an exception
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
private static final Logger log = LoggerFactory.getLogger(Twitter.class);
} catch (TwitterException e) {
// It prints the message as well as the exception
// log.error("Unable to show status", e);
// I would like to pass a status as well 开发者_如何学运维as an exception
// Is this an appropriate log statement
String status = "failed";
log.error("Unable to show status {}", status, e);
}
The above log.error statement is a variant of log.error, will the above statement work properly. I am not sure since I am passing "status" also. Kindly clarify
If unsure, simply use String#format
to create the log message:
log.error(String.format("Unable to show status %s", status), e);
精彩评论