java slf4j 使用实例_SLF4J参数化日志消息

本文详细介绍了SLF4J如何使用参数化日志消息,包括语法、实例演示和优势。通过在日志消息中使用占位符并传递相应值,可以提高日志记录效率,避免在日志级别未启用时进行不必要的计算。示例展示了如何使用单个、两个及多个参数进行日志记录,并解释了参数化日志在节省资源方面的优势。
摘要由CSDN通过智能技术生成

本篇文章帮大家学习SLF4J参数化日志消息,包含了SLF4J参数化日志消息使用方法、操作技巧、实例演示和注意事项,有一定的学习价值,大家可以用来参考。

正如本教程前面所讨论的,SLF4J提供了对参数化日志消息的支持。可以在消息中使用参数,并在稍后的同一语句中将值传递给它们。

语法如下所示,需要在消息(String)中的任何位置使用占位符({}),稍后可以在对象形式中为占位符传递值,并使用逗号分隔消息和值。

Integer age;

Logger.info("At the age of {} I go to school.", 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 {} I go to school.", age);

}

}

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

Dec 10, 2019 13:27:05 PM PlaceHolders main

INFO: At the age of 23 I go to school.

参数化日志的优势

在java中,如果需要在语句中打印值,可使用连接运算符 -

System.out.println("At the age of "+23+" I go to school.");

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

如果它是一个日志记录语句,并且如果语句的特定日志级别被禁用,则所有这些计算都没有用。

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

例如,如果有一个语句为 -

Integer age;

Logger.debug("At the age of {} I go to school.", age);

只有在启用了调试的情况下,SLF4J才会将age转换为整数并将其与字符串连接,否则它什么都不做。因此,禁用日志记录级别时会产生参数构造的成本。

两个参数变体

还可以在消息中使用两个参数 -

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

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

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:

86

Enter new weight:

77

Dec 20, 2019 4:12:21 PM PlaceHolders main

INFO: Old weight is 86. new weight is 77.

Dec 20, 2019 4:12:21 PM PlaceHolders main

INFO: After the program weight reduced is: 9

多个参数变体

还可以使用两个以上的占位符,如以下示例所示 -

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

public class PlaceHolders {

public static void main(String[] args) {

Integer age = 23;

String designation = "Software Engineer";

String company = "Yiibai";

//Creating the Logger object

Logger logger = LoggerFactory.getLogger(Sample.class);

//Logging the information

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

}

}

执行上面示例代码,得到以下结果:

Dec 10, 2019 8:43:52 PM main

INFO: At the age of 23 I got his first job as a Software Engineer at Yiibai

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值