Slf4j 简明教程

SLF4J - Parameterized logging

正如本教程前面讨论的,SLF4J 提供对参数化日志消息的支持。

您可以在消息中使用参数,并在同一语句的后面为它们传递值。

Syntax

如下所示,您需要在消息(字符串)中使用占位符({}),只要您需要在 object 形式中传递值,并使用逗号分隔消息和值。

Integer age;
Logger.info("At the age of {} ramu got his first job", age);

Example

以下示例演示使用 SLF4J 的参数化日志记录(带单个参数)。

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class PlaceHolders {
   public static void main(String[] args) {

      //Creating the Logger object
      Logger logger = LoggerFactory.getLogger(PlaceHolders.class);
      Integer age = 23;

      //Logging the information
      logger.info("At the age of {} ramu got his first job", age);
   }
}

Output

执行后,上述程序生成以下输出 -

Dec 10, 2018 3:25:45 PM PlaceHolders main
INFO: At the age of 23 Ramu got his first job

Advantage of Parameterized Logging

在 Java 中,如果我们需要在语句中打印值,我们将使用连接运算符,如下所示:

System.out.println("At the age of "+23+" ramu got his first job");

这涉及将整数值 23 转换为字符串,并将该值连接到它周围的字符串。

如果这是一个日志语句,并且该语句的特定日志级别被禁用,则所有这些计算都将毫无用处。

在这种情况,您可以使用参数化日志记录。在该格式中,SLF4J 最初确认是否启用了特定级别的日志记录。如果是,则它用各自的值替换消息中的占位符。

例如,如果我们有一个语句,如

Integer age;
Logger.debug("At the age of {} ramu got his first job", age);

只有在启用调试时,SLF4J 才会将年龄转换成整数,并在字符串连接它,否则它将不执行任何操作。因此,如果禁用日志记录级别,将导致参数构造成本。

Two Argument Variant

您还可以在消息中使用两个参数,如下所示:

logger.info("Old weight is {}. new weight is {}.", oldWeight, newWeight);

Example

以下示例演示了带参数日志记录中两个占位符的用法。

import java.util.Scanner;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class PlaceHolders {
   public static void main(String[] args) {
      Integer oldWeight;
      Integer newWeight;
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter old weight:");
      oldWeight = sc.nextInt();

      System.out.println("Enter new weight:");
      newWeight = sc.nextInt();

      //Creating the Logger object
      Logger logger = LoggerFactory.getLogger(Sample.class);

      //Logging the information
      logger.info("Old weight is {}. new weight is {}.", oldWeight, newWeight);

      //Logging the information
      logger.info("After the program weight reduced is: "+(oldWeight-newWeight));
   }
}

Output

执行后,上述程序生成以下输出。

Enter old weight:
85
Enter new weight:
74
Dec 10, 2018 4:12:31 PM PlaceHolders main
INFO: Old weight is 85. new weight is 74.
Dec 10, 2018 4:12:31 PM PlaceHolders main
INFO: After the program weight reduced is: 11

Multiple Argument Variant

您还可以使用多个占位符,如下面的示例所示:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class PlaceHolders {
   public static void main(String[] args) {
      Integer age = 24;
      String designation = "Software Engineer";
      String company = "Infosys";

      //Creating the Logger object
      Logger logger = LoggerFactory.getLogger(Sample.class);

      //Logging the information
      logger.info("At the age of {} ramu got his first job as a {} at {}", age, designation, company);
   }
}

Output

执行后,上述程序生成以下输出 -

Dec 10, 2018 4:23:52 PM PlaceHolders main
INFO: At the age of 24 ramu got his first job as a Software Engineer at Infosys