以下是一些记录方法。 使用SLF4J:
这样做的最佳/正确方法是什么?
log.debug(John + " has " + number +" apples with him");
log.debug("{} has {} apples with him", John, number);
log.debug(format("%s has %d apples with him", John, number));
答案 0 :(得分:1)
第二个选项,
log.debug("{} has {} apples with him", John, number);
是“正确”的方式。
引入了参数化日志记录,以避免在甚至未启用消息时计算格式化字符串的费用。这要求您将格式规范和必要的参数传递给记录器。如果启用了记录器,则记录器仅完成格式化操作。
另外两个选项会抢先格式化消息,即使它不会被记录。