Java log introduction - commons logging

Posted by jakobdoppler on Fri, 06 Mar 2020 05:11:47 +0100

Apache Commons Logging(JCL) provides a simple log abstraction that allows developers to use specific log implementations. JCL can use other log implementations, including Log4J, Avalon LogKit(Avalon's log framework), and JDK logging(JUL). This paper mainly introduces the simple use of JCL, the software version used in this paper: Java 1.8.0, commons logging 1.2.

1, configuration

JCL can automatically find the configuration file for configuration during initialization; the priority is as follows:

1. Find the configuration file commons-logging.properties under classpath, and use the Log implementation class defined by org.apache.commons.logging.Log property in the file
2. Find the Log implementation class corresponding to the system environment variable org.apache.commons.logging.Log
3. Check whether there is a package of Log4j in the classpath. If it is found, Log4j will be automatically used as the log implementation class (org.apache.commons.logging.impl.Log4JLogger)
4. Use JDK's own log implementation class (there will be a log implementation class only after JDK 1.4) (org.apache.commons.logging.impl.Jdk14Logger)
5. Use a simple log implementation class simplelog provided by commons logging (org. Apache. Commons. Logging. Impl. Simplelog)

2,spring-jcl

JCL works well with log4j and JDK logging(JUL); if you want to use logback, log4j2 and other new logging frameworks, you need to use adapters. Spring provides an adapter spring JCL:

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-jcl</artifactId>
  <version>5.2.4.RELEASE</version>
</dependency>

The priority of the adapter using the log framework is as follows:

1. Check whether there is a package of log4j2 in the classpath. If there is, check whether there is a package of slf4j and log4j to slf4j. If there are, use slf4j. Otherwise, use log4j2
2. Check whether there is a slf4j package in the classpath, and if so, use slf4j
3. None of the above uses JDK logging

For specific logic, see the static module in the source org.apache.commons.logging.LogAdapter:

private static final String LOG4J_SPI = "org.apache.logging.log4j.spi.ExtendedLogger";

private static final String LOG4J_SLF4J_PROVIDER = "org.apache.logging.slf4j.SLF4JProvider";

private static final String SLF4J_SPI = "org.slf4j.spi.LocationAwareLogger";

private static final String SLF4J_API = "org.slf4j.Logger";


private static final LogApi logApi;

static {
    if (isPresent(LOG4J_SPI)) {
        if (isPresent(LOG4J_SLF4J_PROVIDER) && isPresent(SLF4J_SPI)) {
            // log4j-to-slf4j bridge -> we'll rather go with the SLF4J SPI;
            // however, we still prefer Log4j over the plain SLF4J API since
            // the latter does not have location awareness support.
            logApi = LogApi.SLF4J_LAL;
        }
        else {
            // Use Log4j 2.x directly, including location awareness support
            logApi = LogApi.LOG4J;
        }
    }
    else if (isPresent(SLF4J_SPI)) {
        // Full SLF4J SPI including location awareness support
        logApi = LogApi.SLF4J_LAL;
    }
    else if (isPresent(SLF4J_API)) {
        // Minimal SLF4J API without location awareness support
        logApi = LogApi.SLF4J;
    }
    else {
        // java.util.logging as default
        logApi = LogApi.JUL;
    }
}

If both commons logging and spring JCL are used in the classpath, spring JCL is preferred.

3. Actual use

3.1. Configuration file commons-logging.properties

This file can be placed under src or src/main/resources(spring boot project).

org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger
#org.apache.commons.logging.Log=org.apache.commons.logging.impl.SimpleLog

3.2 code example

package com.inspur.demo.log;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * commons-logging Use example
 */
public class CommonsLoggingCase {

    public static void main(String[] args) {
        Log log = LogFactory.getLog(CommonsLoggingCase.class);
        System.out.println(log.getClass());
        log.debug("This is debug message.");
        log.warn("This is warn message.");
        for (int i = 0; i < 100; i++) {
            log.info("This is info message:" + i);
        }
    }
}

1. Introduce the jar package of Commons logging and log4j, 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

2. The jar packages of spring JCL and log4j2 are introduced, and the jars of Commons logging are deleted (or not deleted, spring JCL is preferred), and log4j2 is configured with log4j2.xml. The running code can see that the log format is consistent with that configured in log4j2.xml. For the configuration of log4j2, please refer to: Java log Introduction (4)-Log4j2

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.

Topics: Java log4j Apache Spring JDK