Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the logging Framework in Spring Boot 2.x

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

How to use the log framework in Spring Boot 2.x, this article introduces the corresponding analysis and answer in detail, hoping to help more partners who want to solve this problem to find a more simple and easy way.

Why SLF4J?

By default, Spring Boot uses SLF4J + Logback to log and output to the console at the INFO level.

SLF4J, that is, simple log facade (Simple Logging Facade for Java), can be seen as an interface framework, and does not provide a specific implementation, the specific implementation is completed by logging frameworks such as Log4j,Log4j2,Logback and so on.

Why should we use SLF4J's API in practical applications?

It is also clearly [mandatory] stipulated for us in Alibaba's development manual.

The Baidu encyclopedia also gives the corresponding explanation:

SLF4J makes your code independent of any particular logging API, which makes our program more robust and won't distract you from problems caused by different logging implementations.

Another reason to use SLF4J is that it supports placeholders:

String name = "Vi"; logger.debug ("My Name is {}", name)

This program will not get the name until it is running. This not only reduces many string concatenations in the code, but also reduces the heap memory resources required to create String objects.

How to use SLF4J?

If we use SLF4J in a Spring Boot program, we don't need to introduce other packages, because the jar package about slf4j is already included in the spring-boot-starter we have introduced. We just need to get it through LoggerFactory:

Import org.slf4j.Logger;import org.slf4j.LoggerFactory;...private static final Logger logger = LoggerFactory.getLogger (ResponseAop.class)

Here we need to pay attention to whether we choose the Logger under the slf4j package, don't make a mistake.

Use @ Slf4j with Lombok

First add the dependency of Lombok to pom.xml:

Org.projectlombok lombok true

Then open the settings for IDEA and look in Plugins for:

Remember to restart after installing the plug-in to make it take effect!

Then, we can use it in the project like this:

@ Slf4jpublic class Slf4JTest {public static void main (String [] args) {log.info ("This is a Slf4jTest");} the answer to the question on how to use the log framework in Spring Boot 2.x is shared here. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report