Analysis of java log framework slf4j

Many open source code logging frameworks use slf4j, and slf4j (Simple Loging Facade For Java) literally translates to a simple java log facade, which is a log output interface provided by Java rather than a log implementation solution. Therefore, slf4j needs to be used with other log frameworks, such as log4j, logback, etc.
1. The advantages of using slf4j over directly using other log implementation solutions to realize the log function
In the actual framework construction and code writing process, we will often use other frameworks, and different frameworks are implemented by different coders or organizations. Logging frameworks also vary. At this time, after we refer to other frameworks, the log frameworks in the code will be various, so that there will be many problems. The slf4j interface implementation idea is similar to the JVM framework or JDBC (of course it is much simpler than JVM and JDBC), it shields the specific implementation details of the log, and when we refer to other log solutions based on slf4j implementation, it can be very good Convert to the logging scheme we are using now. In this way, we can achieve flexible switching of the log system during development.
2. How to use slf4j with other log schemes (here, take springboot+slf4j+logback as an example)
1. Introduce jar package

   <properties>  
        <slf4j-api>1.7.25</slf4j-api>
        <logback-core>1.2.3</logback-core>
        <logback-access>1.1.3</logback-access>
        <logback-classic>1.2.3</logback-classic> 
    </properties> 
----------
<dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j-api}</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-core</artifactId>
            <version>${logback-core}</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-access</artifactId>
            <version>${logback-access}</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>${logback-classic}</version>
        </dependency>

2. The logback configuration is the same as using logback alone

<?xml version="1.0" encoding="UTF-8" ?>

<configuration scan="true" debug="false">
    <!-- 应用名称 -->
    <property name="APP_NAME" value="kpatch_window" />

    <!-- <property name="LOG_HOME" value="${log.dir:-logs}/${APP_NAME}" /> -->
    <property name="LOG_HOME" value="C:/kpatchlogs/${APP_NAME}" />
    <!-- 服务器端日志目录 -->
    <!-- <property name="LOG_HOME" value="/home/donald/apache-tomcat-7.72.0/logs"></property> -->

    <!-- 日志输出格式 -->
    <property name="ENCODER_PATTERN"
        value="%d{yyyy-MM-dd  HH:mm:ss.SSS} [%thread] %-5level %logger{80} - %msg%n" />
    <contextName>${APP_NAME}</contextName>

    <!-- 控制台 -->
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger %L- %msg%n
            </pattern>
        </encoder>
    </appender>


    <!-- 系统错误日志:用于将错误日志输出到独立文件 -->
    <appender name="SYSTEM_ERROR_FILE"
        class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${LOG_HOME}/error.%d{yyyy-MM-dd}.log
            </fileNamePattern>
            <maxHistory>7</maxHistory>
        </rollingPolicy>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>${ENCODER_PATTERN}</pattern>
        </encoder>
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>ERROR</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
    </appender>


    <appender name="NOT_CONTAIN_ERROR"
        class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!-- 按天回滚 daily -->
            <fileNamePattern>${LOG_HOME}/info.%d{yyyy-MM-dd}.log
            </fileNamePattern>
            <maxHistory>60</maxHistory>
        </rollingPolicy>
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n
            </pattern>
        </encoder>
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>ERROR</level>
            <onMatch>DENY</onMatch>
            <onMismatch>ACCEPT</onMismatch>
        </filter>
    </appender>

    <root level="INFO">
        <appender-ref ref="STDOUT" />
        <appender-ref ref="NOT_CONTAIN_ERROR" />
    </root>
    <!-- 根日志 -->
    <logger name="system">
        <appender-ref ref="SYSTEM_ERROR_FILE" />
    </logger>


</configuration>

3. When using logger, it should be noted that the Logger class and LoggerFactory class we use are in the slf4j package

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value="/test")
public class TestController {

    private static final Logger log = LoggerFactory.getLogger(TestController.class);


}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325564312&siteId=291194637