SLF4J log facade technology

Personally combine learning videos to make a simple understanding and record

Which log implementation is used for the slf4j log facade depends on the order of importing dependencies in the pom file, and whoever is in the front uses whoever is used as the log implementation

core --slf4j dependency

        <!--slf4j 核心依赖-->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.25</version>
        </dependency>

1. Getting Started Case

On the basis of not integrating other log frameworks, slf4j uses the built-in log framework slf4j-simple, and dependencies must be introduced separately

        <!--slf4j 自带的简单日志实现 -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>1.7.25</version>
        </dependency>

display effect

64541ee12dca4cb9b13346c4ebdaa6b5.png

 1.1 It is also possible to dynamically output log information, which is more flexible, by replacing string concatenation with placeholders

        Logger logger = LoggerFactory.getLogger(SLF4JTest01.class);
        String name = "zs";
        int age = 23;
        //{},这个括号就是占位符
        logger.info("学生信息-姓名:"+name+";年龄:"+age);
        logger.info("学生信息-姓名:{},年龄:{}",new Object[]{name,age});
        logger.info("学生信息-姓名:{},年龄:{}",name,age);

Displayed log information

0d5f593e56fb49ddb8059fcdb4194dc8.png

 2. Before integrating the third-party log framework, take a look at the official website of slf4j

ca5f2271f6a64fd0a5db195e0b2c5e07.png

 2.1 The slf4j log facade has 3 situations to bind the log implementation

        1. On the basis of not binding any log implementation, the log facade cannot implement any functions. slf4j-simple is the log implementation provided by the slf4j official website. After importing dependencies, it is automatically bound to the slf4j log facade. If not imported, it will No implementation is provided.

        2. Logback and simple (including nop) are both log implementations provided behind the timeline of the slf4j log facade, so the design is completely followed by slf4j, and it can be seamlessly connected only by importing dependencies. The role of nop is to prohibit all logs Print.

        3. Both log4j and JUL are log implementations in front of the timeline of the slf4j log facade, so the API does not follow the design of slf4j, and can complete the connection with the log facade by adapting the bridging technology.

logback-dependency

        <!-- logback依赖 -->
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.2.3</version>
        </dependency>

nop-dependent

        <!-- 导入nop -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-nop</artifactId>
            <version>1.7.25</version>
        </dependency>
//--------------------------分割线----------------------------------------------------------

logback, simple, and nop belong to the emergence of slf4j log facade technology, so they can be used only by directly importing dependencies.

3. At this time, if we need to use log4j log implementation, we need to use bridging technology, bind an adapter, and import two dependencies to realize the function

        Adapter dependencies and log4j dependencies

        <!-- 导入log4j适配器依赖 -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.25</version>
        </dependency>

        <!-- 导入log4j依赖 -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>

You also need to add the log4j configuration file

#配置根节点logger
log4j.rootLogger=trace,console

log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.conversionPattern=[%-10p]%r %c%t%d{yyyy-MM-dd HH:mm:ss:SSS} %m%n

log display information

cf118964aded460893ef5ac1ca033541.png

 4. JUL log implementation-belongs to the log implementation before the slf4j timeline, and needs to add adapter dependencies. Because JUL is built in JDK, there is no need to manually import dependencies

        <!--导入JUL的适配器依赖,因为JUL是JDK内置的,所以不用导入JUL的依赖-->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-jdk14</artifactId>
            <version>1.7.25</version>
        </dependency>

log display information

7b649350c5c247ccabf521017dc0d670.png

 

 Remarks: When such a warning message in the red box appears, it means that multiple log implementations have been imported, but only one (the first in order) is used by default, and it will not be displayed if it is commented out. However, in a real production environment, generally only one log implementation is bound, and redundant warning messages will appear if more than one is bound.

955761154a3744d486566ccd7f9e44af.png

 

5. demo

Requirements: log4j is used in the project. Now log4j cannot meet the needs of the project. It needs to be refactored to slf4j+logback to solve this problem without changing the code.

        5.1 Comment out log4j. At this time, the code must report an error. Then we add the dependencies of slf4j and logback, and then add a dependency of the bridge. The bridge solves the problem of log reconstruction in the project. When there are previous logs in the project API, which can be converted to the implementation of slf4j through a bridge.

        <!--slf4j 核心依赖-->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.25</version>
        </dependency>
        <!--桥接器-->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>log4j-over-slf4j</artifactId>
            <version>1.7.25</version>
        </dependency>
        <!-- logback依赖 -->
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.2.3</version>
        </dependency>

 Log information before reconstruction

09f1028955774504979c249db4be29c5.png

 Refactored log information

09ff5e6a09aa428cb0f829cebe4cfb11.png

 After refactoring, it will create an illusion that the component resources under the log4j package are used, but the real log implementation is the slf4j facade + logback log implementation, which is the effect brought by the bridge.

Notice:

        After the bridge is joined, the adapter is not necessary to join

        Bridges and adapters cannot join at the same time

        If the bridge is above the adapter, an error will be reported when running

        If the bridge is below the adapter, the bridge will not be executed, meaningless

 

Guess you like

Origin blog.csdn.net/DDDM456/article/details/127390193