The development of JAVA log and the use of springboot log

1, Introduce the history of log development

  • jdk1.3 System.out.println("") tracing
    It can only be used for debugging when writing code. After online deployment, it is difficult to locate the problem and find the problem. System.out.println("") will only print logs on the console. After the logs are overstocked, the historical logs cannot be queried.
  • logUtil
    Compared with System.out.println(""), it is improved to write the log to the file for easy positioning. But there are still many deficiencies to be improved
  • log4j
    logUtil has been improved
    1. In order to meet the needs of different environments, reduce unnecessary log output and increase the division of log levels. (tracking 1 Information 2 debugging 3 exception 4)
    2. If the log file is too large, can it be stored in separate files, and can the save time be set for some unimportant logs
    3. IO interaction is required to record log files, and whether asynchronous storage can be performed.
    ...
  • Other log frames
    With the popularity of log4j log framework, other log frameworks were born. For example, Jul (provided by JDK), log4j simple, log4j nop, etc
  • Log facade
    Because there are many kinds of log frames, there are many kinds of projects to use. At this time, the log facade came into being.
    Log facade: it does not realize the log function, but integrates logs.
    The first is JCL developed by JDK. Later, other developers developed slf4j
    slf4j has better relative performance
  • Optimization of log4j logging framework
    log4j has comprehensive functions and high market share, but there is another disadvantage: poor performance.
    apatch: optimize the performance of log4j and develop log4j2
    Log4j developer: optimize the performance of log4j and develop logback

Summary: log4j has stopped updating because of performance problems. You can use log4j2 and logback directly. The facade can be directly selected slf4j. Slf4j and logback are developed by the same author, so logback is recommended by the logging framework

2, Log instance

1. Use log framework directly

a.Jul log framework usage
Since the JDK is officially developed and archived in the JDK standard library, there is no need to introduce dependency

public class JulController {
    public static void main(String[] args) {
        Logger logger = Logger.getLogger(JulController.class.getName());
        logger.info("JDK Official log framework");
    }
}

b.Log4j log framework usage

<dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
/**
 * Open source logging framework Log4j
 */
public class Log4jController {
    public static void main(String[] args) {
        Logger logger = Logger.getLogger(Log4jController.class);
        logger.info("Open source logging framework Log4j");
    }
}

You also need to add the configuration file log4j.properties: specify the log printing level, storage location, log format, etc

#trace<debug<info<warn<error<fatal
log4j.rootLogger=trace, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] ====== %m%n

2. Use log facade

a.JCL facade use

<!-- introduce JCL Facade dependence -->
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.2</version>
        </dependency>
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.logging.Logger;

/**
 * JDK Official log framework
 *
 */
public class JulController {
    public static void main(String[] args) {
        /*Logger logger = Logger.getLogger(JulController.class.getName());
        logger.info("JDK Official log framework "");*/
        Log log = LogFactory.getLog(JulController.class);
        log.info(log.getClass()+"JDK Official log framework");
    }
}

After proceeding as above, observe the printed console and find that the Jul log framework may not be used
Reason: JCL dynamic lookup mechanism instantiates logs in the order of commons- ­ logging.properties ­­­­> System environment variable ­­­­­­­> log4j ­­­> jul ­­­ > simplelog ­­­­> nooplog
Solution: add the commons-logging.properties configuration file to specify which logging framework to bridge

org.apache.commons.logging.Log = org.apache.commons.logging.impl.Jdk14Logger

b.slf4j facade use

Add the dependency of slf4j and the adapter for log4j

<!-- slf4j Core dependency -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.30</version>
        </dependency>
        <!-- slf4j in the light of log4j Adapter for -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.30</version>
        </dependency>
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Open source logging framework Log4j
 */
public class Log4jController {
    public static void main(String[] args) {
        /*Logger logger = Logger.getLogger(Log4jController.class);
        logger.info("Open source logging framework Log4j ");*/
        Logger logger = LoggerFactory.getLogger(Log4jController.class);
        logger.info(logger.getClass()+"Open source logging framework Log4j");
    }
}

3. Conversion between log faces

For the unified implementation of logs, the JCL facade is converted to SLF4J

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

When you run the main method of JulController above, you will find that the log framework of Log4j is used

3, Implementation of log in spring boot

The bottom layer of SpringBoot also uses slf4j+logback for logging
Add springboot dependency

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-parent</artifactId>
        <version>2.1.2.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

springboot log document link: https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.logging
1. Modify log level
Modify profile

logging:
  level:
  #Specify that the log level under this package is: tracking, and it is the default under other packages
    com.tedu.logging: "trace"

Keywords: Java Spring Spring Boot

Added by FeeBle on Sun, 10 Oct 2021 09:15:33 +0300