Slf4j-parameterized-logging
SLF4J-パラメーター化されたロギング
このチュートリアルで前述したように、SLF4Jはパラメーター化されたログメッセージのサポートを提供します。
メッセージでパラメーターを使用し、後で同じステートメントで値を渡すことができます。
構文
以下に示すように、メッセージ(文字列)で必要な場所でプレースホルダー(\ {})を使用する必要があり、後で object 形式でプレースホルダーの値を渡すことができます。
Integer age;
Logger.info("At the age of {} ramu got his first job", age);
例
次の例は、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);
}
}
出力
実行時に、上記のプログラムは次の出力を生成します-
Dec 10, 2018 3:25:45 PM PlaceHolders main
INFO: At the age of 23 Ramu got his first job
パラメータ化されたロギングの利点
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は年齢を整数に変換し、それ以外の場合は文字列と連結します。 したがって、ロギングレベルが無効になっている場合、パラメーター構築のコストが発生します。
2つの引数のバリアント
また、メッセージで2つのパラメータを使用することができます-
logger.info("Old weight is {}. new weight is {}.", oldWeight, newWeight);
例
次の例は、パラメーター化されたロギングでの2つのプレースホルダーの使用方法を示しています。
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));
}
}
出力
実行すると、上記のプログラムは次の出力を生成します。
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
複数引数バリアント
また、次の例に示すように3つ以上のプレースホルダーを使用することができます-
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);
}
}
出力
実行時に、上記のプログラムは次の出力を生成します-
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