The SLF4J logging framework is used in the project

introduce

The full name of SLF4J is "Simple Logging Facade for Java", which serves as a simple facade for various logging frameworks. For example: java.util.logging, logback, reload4j, etc. The logging framework can be switched only by switching the jar package dependencies of the logging framework.

The logging framework supported by SLF4J includes the following:

  • log4j: a commonly used log framework, which requires a configuration file log4j.properties
  • logback: You can modify the log output configuration through logback.xml, and use the default configuration if there is no logback.xml
  • java.util.logging:JDK 1.4 logging
  • simple: simple implementation, only display levels above INFO, actually use System.err output
  • jcl:Jakarta Commons Logging
  • nop: Do not display all logs silently

Example of use

1. Introduce dependent jar packages

SLF4J has released the 2.X version at present, here is the frequently used version 1.7.36 as an example.

Create a simple maven project and introduce dependencies.

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.36</version>
</dependency>
<!-- log4j -->
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-reload4j</artifactId>
    <version>1.7.36</version>
</dependency>

<!-- logback
<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>1.2.11</version>
</dependency> -->

<!-- 简单实现,只显示INFO以上级别,实际使用System.err输出
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-simple</artifactId>
    <version>1.7.36</version>
</dependency> -->

<!-- jdk14
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-jdk14</artifactId>
    <version>1.7.36</version>
</dependency> -->

<!-- nop 无提示不显示所有日志
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-nop</artifactId>
    <version>1.7.36</version>
</dependency>-->

<!-- Jakarta Commons Logging
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-jcl</artifactId>
    <version>1.7.36</version>
</dependency> -->

2. Example of code usage

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

public class LogTest {
    
    
    private final static Logger log = LoggerFactory.getLogger(LogTest.class);
    public static void main(String[] args) {
    
    
        log.info("这个是测试日志输出,{}","Hello World");
    }
}

1) If you use log4j, you need to add a configuration file log4j.properties. If there is no such configuration file, the log cannot be printed. The simple content is as follows:

# Root logger option
log4j.rootLogger=INFO,file,stdout

# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%t] %-5p %c{2}:%L - %m%n


# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=log/output-log4j.log
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%t] %-5p %c{2}:%L - %m%n

2) If you use logback, you can also add a configuration file logback.xml. If there is no such configuration file, use the default configuration. The content is as follows:

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

    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>

    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n</pattern>
            <charset>utf-8</charset>
        </encoder>
        <file>log/output-logback.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
            <fileNamePattern>log/output.log.%i</fileNamePattern>
        </rollingPolicy>
        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <MaxFileSize>1MB</MaxFileSize>
        </triggeringPolicy>
    </appender>

    <root level="INFO">
        <appender-ref ref="CONSOLE" />
        <appender-ref ref="FILE" />
    </root>
</configuration>

3. Test the printed content

1) log4j

2023-03-10 14:55:54 [main] INFO  demo.LogTest:9 - 这个是测试日志输出,Hello World
2023-03-10 14:55:56 [main] INFO  demo.LogTest:9 - 这个是测试日志输出,Hello World

2)logback

2023-03-10 14:53:21 [main] INFO cn.hj.sl4j.demo.LogTest - 这个是测试日志输出,Hello World
2023-03-10 14:53:33 [main] INFO cn.hj.sl4j.demo.LogTest - 这个是测试日志输出,Hello World

3)slf4j-simple

[main] INFO cn.hj.sl4j.demo.LogTest - 这个是测试日志输出,Hello World

4) slf4j-jdk14

三月 10, 2023 3:00:53 下午 cn.hj.sl4j.demo.LogTest main
信息: 这个是测试日志输出,Hello World

5)jcl

三月 10, 2023 3:01:31 下午 org.slf4j.impl.JCLLoggerAdapter info
信息: 这个是测试日志输出,Hello World

Summarize

SLF4J is a facade log framework, which unifies the usage standard of logs. If you want to switch between different log implementations, you only need to modify the corresponding implementation dependent jar package, and you don’t need to modify the java code.

Guess you like

Origin blog.csdn.net/wlddhj/article/details/129443401
Recommended