Java log Introduction (6,end)-SLF4J

The Simple Logging Facade for Java(SLF4J) provides the abstraction of various logging frameworks (such as java.util.log, logback, log4j), allowing users to introduce the required logging framework during deployment; its functions are similar to those of Commons logging, but SLF4J adapts a broader logging framework. The software version used in this paper: Java 1.8.0, slf4j 1.7.29.

1. SLF4J binding log framework

SLF4J provides multiple "SLF4J binding" jar packages. Each binding corresponds to a supported log framework.

1.1,slf4j-log4j12

To bind log4j 1.2, you need to introduce the jar package of log4j 1.2.x.

<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-log4j12</artifactId>
  <version>1.7.30</version>
</dependency>

1.2,slf4j-jdk14

Binding java.util.logging(jul)

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

1.3,slf4j-nop

Bind nop and discard all logs.

<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-nop</artifactId>
  <version>1.7.30</version>
</dependency>

1.4,slf4j-simple

Binding is a simple implementation that outputs all events to System.err. Only messages of level INFO and higher are printed. This binding can be useful in the context of small applications.

<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-simple</artifactId>
  <version>1.7.30</version>
</dependency>

1.5,slf4j-jcl

Bind Jakarta Commons Logging to delegate all SLF4J logging to JCL.

<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-jcl</artifactId>
  <version>1.7.30</version>
</dependency>

1.6,logback-classic

logback itself implements binding slf4j.

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

 

The following is a general diagram:

1.7. Log4j slf4j impl (or log4j slf4j18 impl)

Bind log4j2. Since log4j2 appears later, the jar package is not provided by SLF4J, and is provided by log4j2's own implementation.

<dependency>
  <groupId>org.apache.logging.log4j</groupId>
  <artifactId>log4j-slf4j-impl</artifactId>
  <version>2.12.1</version>
</dependency>
<dependency>
  <groupId>org.apache.logging.log4j</groupId>
  <artifactId>log4j-slf4j18-impl</artifactId>
  <version>2.12.1</version>
</dependency>

log4j-slf4j-impl is used for SLF4J 1.7.x or earlier, and log4j-slf4j18-impl is used for SLF4J 1.8.x or later.

2. Transfer other logs to SLF4J

In this case, the specific log is converted to SLF4J to output the log. It cannot be used at the same time with the corresponding jar bound in 1, otherwise it will cause a dead cycle of log events.

2.1,jcl-over-slf4j

When jcl is converted to slf4j, the jar package of Commons logging is replaced by jcl over slf4j, which cannot be used at the same time as slf4j jcl.

<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>jcl-over-slf4j</artifactId>
  <version>1.7.30</version>
</dependency>

2.2,log4j-over-slf4j

When log4j is converted to slf4j, the jar package of log4j12 is replaced by log4j over slf4j, which cannot be used at the same time as slf4j log4j12.

<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>log4j-over-slf4j</artifactId>
  <version>1.7.30</version>
</dependency>

2.3,jul-to-slf4j

The jar package of jul-to-slf4j can not be used at the same time as slf4j-jdk14.

<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>jul-to-slf4j</artifactId>
  <version>1.7.30</version>
</dependency>

Add the following two lines of code before using jul to record logs, and the output will be slf4j:

SLF4JBridgeHandler.removeHandlersForRootLogger();
SLF4JBridgeHandler.install();

 

The following is a general diagram:

2.4,log4j-to-slf4j

Log4j2 is converted to slf4j, and the jar package of log4j to slf4j is introduced. The jar package is not provided by slf4j and is implemented by log4j2 itself. It cannot be used at the same time with log4j-slf4j-impl (or log4j-slf4j18-impl).

<dependency>
  <groupId>org.apache.logging.log4j</groupId>
  <artifactId>log4j-to-slf4j</artifactId>
  <version>2.12.1</version>
</dependency>

3. Actual use

package com.inspur.demo.log;

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


/**
 * SLF4J Use example
 */
public class SLF4JCase {
    public static final Logger logger = LoggerFactory.getLogger(SLF4JCase.class);
    public static void main(String[] args) throws Exception {
        System.out.println(logger.getClass());
        logger.debug("This is debug message.");
        logger.warn("This is warn message.");
        for (int i = 0; i < 100; i++) {
            logger.info("This is info message:" + i);
        }
    }
}

1. The jar package of slf4j and logback is introduced, and logback.xml is used to configure logback. The running code can see that the log format is consistent with that configured in logback.xml. For logback configuration, please refer to: Java log Introduction (3)-Logback

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.29</version>
</dependency>
<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>1.2.3</version>
</dependency>

2. Introduce the jar package of slf4j, log4j, slf4j-log4j12, and use log4j.properties to configure log4j. Run the code to see that the log format is consistent with that configured in log4j.properties. For log4j configuration, please refer to: Java log Introduction (2)-Log4j

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.29</version>
</dependency>
<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.7.29</version>
</dependency>

 

If you want to use other logging frameworks, you can add relevant jar s and run code tests by yourself, which will not be demonstrated here.

Tags: Java log4j Apache xml

Posted on Thu, 12 Mar 2020 06:00:40 -0400 by tyrol_gangster