Spring日志处理——logger占位符

背景

  • 在开发活动中,,记日志是一个很常见的操作, 记日志经常涉及到拼接字符串,这时我们就可以使用到占位符表示要拼接的字符串

方法

  • 使用方法如下
public static void main(String[] args) {
        List<String> actualHpList = new LinkedList<>();
        actualHpList.add("1");
        actualHpList.add("2");
        actualHpList.add("3");
        actualHpList.add("4");

        logger.info("actualHpList is {}", actualHpList.toString());
    }

打印结果:16:37:48.199 [main] INFO com.canaan.minemanage.BaseTest - actualHpList is [1, 2, 3, 4]

注意

  • 占位符只能输出字符串,也就意味着,会将占位的参数自动转换成字符串输出
  • 当使用error或者warn级别打印异常时,最好不要使用e.getMessage()和e.getCause()作为参数,因为不是每个异常都打印出异常信息。建议将异常对象作为最后一个参数,不论使用还是不使用占位符,都不会影响异常信息的输出。
原文地址:https://www.cnblogs.com/zuiyue_jing/p/14945240.html