Why do we use logs?

When we first started Java, we used system.out.println () to print the information we wanted to know to the console. However, if we go to run our Java program on the server, this time highlights the importance of the log for a system, search for abnormal information, analysis of the system, etc., all need to use the log.

Why SLF4J?

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

SLF4J, the Simple Logging Facade for Java, can be seen as an interface framework and does not provide a concrete implementation, which is implemented by Logging frameworks such as Log4j, Log4j2, Logback, and so on.

Why should we use the SLF4J API in a real application? In alibaba’s development manual also gave us clear [mandatory] provisions

Baidu Encyclopedia also provides a corresponding explanation:

SLF4J makes your code independent of any particular logging API, making our program more robust without distracting 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);
Copy the code

This program will only get the name at runtime. This not only reduces many String concatenations in the code, but also reduces the heap memory resources needed to create strings.

How to use SLF4J?

If we use SLF4J in a Spring Boot application, we don’t need to import any other packages, because we already include the SLF4J JAR package in the spring-boot-starter package. We just need to get it from LoggerFactory:

import org.slf4j.Logger;
importorg.slf4j.LoggerFactory; .private static final Logger logger = LoggerFactory.getLogger(ResponseAop.class);
Copy the code

The slF4J package Logger should be selected correctly.

Use @slf4j with Lombok

Start by adding Lombok’s dependencies to POM.xml:

        <dependency>
           <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
Copy the code

Then open the IDEA Settings and look in Plugins:

Remember to restart the plug-in after installing it!

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

@Slf4j
public class Slf4JTest {
    public static void main(String[] args) {
        log.info("This is a Slf4jTest"); }}Copy the code

About log configuration

Here I steal a lazy, because a lot of online search, but in fact when you use or according to their own project to use (you can pay attention to me, I will be in the back of the actual project to configure, and give the source code).

One thing to note here: By default, Spring Boot prints logs to the console, not to log files. If you want to write log files other than console output, you need to set the logging.file or logging.path property in application.yml. However, when both properties are configured at the same time, only logging.file is in effect!

The public,

Original article, writing is limited, talent and learning shallow, if the article is not straight, hope to inform.