Overview of logging technology frameworks

  • JUL: logging tool in the JDK, also known as JDKLog or JDK-logging.

  • LOG4J1: A concrete logging implementation framework.

  • LOG4J2: A concrete logging implementation framework, the next version of LOG4J1.

  • LOGBACK: A concrete logging implementation framework, but with better performance.

  • JCL: A logging facade that provides a unified logging interface, also known as Commons-logging.

  • SLF4J: A logging facade that, like JCL, provides a unified logging interface that can be easily switched to look at specific implementation frameworks.

    Note: JUL, LOG4J1, LOG4J2, LOGBACK are the logging implementation frameworks, while JCL, SLF4J are the logging implementation facade (interface).

SLF4J structure overview

Slf4j is only a logging standard, not an implementation of a logging system. It is important to understand that SLF4J does only two things:

  • Log Interface
  • Provides a method to get a concrete log object

As shown in the figure above, SLF4J acts as an adaptation layer for the log printing implementation, and the business code does not need to care about which logging system is used and how to print the log.

SLF4J simple Demo

1. Import the corresponding JAR package

25 <dependency> 26 <groupId>org.slf4j</groupId> 27 <artifactId>slf4j-api</artifactId> 28 <version>1.7.32</version> 29 </dependency> 30 <dependency> 31 <groupId>ch.qos.logback</groupId> 32 <artifactId>logback-classic</artifactId> 33 < version > 1.2.6 < / version > and < / dependency >Copy the code

2. Add the configuration file log4j2. XML to the resource directory:

<? The XML version = "1.0" encoding = "utf-8"? > <configuration> <! -- <include resource="org/springframework/boot/logging/logback/base.xml"/> --> <! -- appenders --> <appender name="DEFAULT-APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${LOG_PATH}/common.log</file> <encoder> <pattern> [%d] [%level] [%X{xRequestId}] %logger - %m%n </pattern> <charset>UTF-8</charset> <! - set character set -- > here < / encoder > < rollingPolicy class = "ch. Qos. Logback. Core. Rolling. TimeBasedRollingPolicy" > <! -- daily rollover --> <fileNamePattern>${LOG_PATH}/common.log.%d{yyyy-MM-dd}</fileNamePattern> </rollingPolicy> </appender> <! -- loggers --> <logger name="com.zenlayer.zenconsole.ocs" level="INFO" additivity="false"> <appender-ref ref="APP-DEFAULT-APPENDER"/> <appender-ref ref="ERROR-APPENDER"/> </logger> <root level="INFO"> <appender-ref ref="DEFAULT-APPENDER"/> <appender-ref ref="ERROR-APPENDER"/> </root> </configuration>Copy the code

3. Test method

private static final Logger logger = LoggerFactory.getLogger(SelectBillingOrderImplTest.class);

@Test
public void logTest() throws IOException {
    logger.info("");
    logger.warn("");
    logger.error("");
}
Copy the code

4. Execution result

SLF4J principle analysis

1. Create a Logger object (Logger is usually defined as a global singleton)

2. GetLogger method

3. Get the strength chemical plant and return the Logger object according to the log class

4. Strength log workshop

5. The bind method is the core of SLF4J

Slf4j: StaticLoggerBinder: org.slf4j.impl: StaticLoggerBinder: org.slf4j.impl: StaticLoggerBinder: org.slf4j.impl: StaticLoggerBinder: There are cases where multiple Log implementations are introduced, so SLf4J returns a Set, but in reality the system loader only loads one StaticLoggerBinder class. So it starts with an error, defaults to the first Log implementation class loaded, and then calls the getSingleton method of the modified class

7. After successfully obtaining the ILoggerFactory above, the logic is to call the Factory’s getLogger method to get the Logger object. Back to source: 3

Find an implementation, and I’ll use LogBack as an example: