Spring boot log Foundation

Keywords: Java log4j Spring Apache SpringBoot

Springboot configuration log configuration file location

logging.config: classpath:logging/logback.yml

Spring boot log classification

log4j: an open source log component implemented by apache

logback: it is also designed by the author of log4j. It has better features. It is a log framework used to replace log4j and is the native implementation of slf4j

log4j2: it is an improved version of log4j 1.x and logback. It adopts some new technologies (no lock asynchrony, etc.) to improve the throughput and performance of the log by 10 times than that of log4j 1.x. it also solves some deadlock bug s, and the configuration is simpler and more flexible

The difference between slf4j+log4j and direct log4j

slf4j is a specification, standard and interface for all log frameworks. It is not a specific implementation of a framework. Because interfaces cannot be used independently, they need to be used in conjunction with specific log framework implementations (such as log4j and logback). The advantage of using interfaces is that when a project needs to change the log framework, it only needs to change the jar and configuration, and does not need to change the relevant java code.

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TestSlf4j {
    //Logger and LoggerFactory import the org.slf4j package
    private final static Logger logger = LoggerFactory.getLogger(TestSlf4j.class);
}

Log4j, logback and log4j2 are all specific log implementation frameworks, so they can be used either alone or in combination with slf4j

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
 
public class TestLog4j {
    // Logger and LogManager import the org.apache.logging package
    private static final Logger LOG = LogManager.getLogger(TestLog4j.class); 
}

Import the required jar package (slf4j+log4j2)

log4j2

If you import the spring boot starter web dependency package in the project, please remove the log dependency of spring

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </exclusion>
    </exclusions>
</dependency>

log4j2 needs to be imported into the springboot project

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>

If you want to use log4j, replace the coordinates of log4j2 with the following one, and still exclude the original spring boot starter logging.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-log4j</artifactId>
    <version>1.3.8.RELEASE</version>
</dependency>

Introduction of log4j configuration file in Java

class ImportConfig{ 
    public static void main(String[] args) throws IOException {  
        File file = new File(log4j2);  
        BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));  
        final ConfigurationSource source = new ConfigurationSource(in);  
        Configurator.initialize(null, source);  
     
        Logger logger = LogManager.getLogger("myLogger");  
    }
}

Springboot integrates zookeeper log4j2

<dependency>
    <groupId>org.apache.zookeeper</groupId>
    <artifactId>zookeeper</artifactId>
    <version>3.4.14</version>

    <exclusions>
        <exclusion>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
        </exclusion>
        <exclusion>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
        </exclusion>
    </exclusions>
</dependency>

Posted by aris1234 on Tue, 28 Apr 2020 19:41:38 -0700