如何使用变量和异常格式化错误日志

时间:2011-08-31 07:19:52

标签: java slf4j twitter4j

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);
        }

上面的log.error语句是log.error的变体,上述语句是否正常工作。我不确定,因为我也传递了“身份”。请澄清

1 个答案:

答案 0 :(得分:6)

如果不确定,只需使用String#format创建日志消息:

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