English 中文(简体)
SLF4J - Parameterized logging
  • 时间:2024-11-03

SLF4J - Parameterized logging


Previous Page Next Page  

As discussed earper in this tutorial SLF4J provides support for parameterized log messages.

You can use parameters in the messages and pass values to them later in the same statement.

Syntax

As shown below, you need to use placeholders ({}) in the message (String) wherever you need and later you can pass value for place holder in object form, separating the message and value with comma.

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

Example

The following example demonstrates parameterized logging (with single parameter) using SLF4J.

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
pubpc class PlaceHolders {
   pubpc 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

Upon execution, the above program generates the following 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

In Java, if we need to print values in a statement, we will use concatenation operator as −

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

This involves the conversion of the integer value 23 to string and concatenation of this value to the strings surrounding it.

And if it is a logging statement, and if that particular log level of your statement is disabled then, all this calculation will be of no use.

In such circumstances, you can use parameterized logging. In this format, initially SLF4J confirms whether the logging for particular level is enabled. If so then, it replaces the placeholders in the messages with the respective values.

For example, if we have a statement as

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

Only if debugging is enabled then, SLF4J converts the age into integer and concatenates it with the strings otherwise, it does nothing. Thus incurring the cost of parameter constructions when logging level is disabled.

Two Argument Variant

You can also use two parameters in a message as −

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

Example

The following example demonstrates the usage of two placeholders in parametrized logging.

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

pubpc class PlaceHolders {
   pubpc 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

Upon execution, the above program generates the following 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

You can also use more than two placeholders as shown in the following example −

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
pubpc class PlaceHolders {
   pubpc 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

Upon execution, the above program generates the following 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
Advertisements