diya
diya

Reputation: 7138

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

Upvotes: 2

Views: 2704

Answers (1)

Andreas Dolk
Andreas Dolk

Reputation: 114777

If unsure, simply use String#format to create the log message:

log.error(String.format("Unable to show status %s", status), e);

Upvotes: 6

Related Questions