Springboot log integration (1): Logback & log4j2

Foreword: Java has many log components, such as log4j, log4j2, logback, and Java Util Logging provided by Java. In fact, springboot provides support for these components. Log4j, log4j2 and logback all provide corresponding component support.

一 、 Logback

1) The logging tool used by default in springboot is logback, but before mentioning the specific logging tool, a term must be mentioned. This term is slf4j (Simple Logging Facade For Java).
2) Baidu Encyclopedia explained:
https://baike.baidu.com/item/slf4j/6408868
3) slf4j is not a specific log solution, it is a bit similar to jdbc, using the facade mode, is an abstraction for all types of logs Implementation, since it is an abstract log implementation, there is definitely no need for additional import in springboot.

Note : Spring-boot-starter provides a dependency on spring-boot-starter-logging, which can be seen in spring-boot-starter-logging and integrates the default support of slf4j and specific implementation of logback:

Insert picture description here
1) Modify UserController

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
private final Logger logger = LoggerFactory.getLogger(UserController.class);

Insert picture description here
2) Enter: localhost:8080/hello on the browser, you can see the output of the console log:

Insert picture description here

1.1, log level

Modify the controller to change the output of the log to logger.debug("this is a hello log");
restart at this time and call again, and it is found that there is no output in the background. The reason is that the log level is at work.

  1. By default, Spring Boot configures the INFO log level, which means it will output logs (ERROR, WARN, INFO) above the INFO level.
    If you need Debug level logs. Configure in src/main/resources/application.properties.
    debug=true
  1. Configure logging.level.* to specifically output the log levels of which packages.
    For example,
    logging.level.root=INFO
    logging.level.org.springframework.web=DEBUG
    logging.level.cn.enjoy.controller=DEBUG

Insert picture description here

  1. At this time, debug logs including springframework.web and cn.enjoy.controller can be output:

Insert picture description here

1.2, log files
  1. Under normal circumstances, springboot logs will only be output to the console, and not written to the log file. However, in some formal environment applications, we need to configure the logging.file file name and logging in the application.properites file. path File path, output the log to the log file.

     logging.path = /var/tmp
     logging.file = xxx.log
     logging.level.root = info
    

note:

If only logging.path is configured, a log file called spring.log is generated in the /var/tmp folder. If only logging.file is configured, a xxx.log log file will be generated in the current path of the project.
There is a pit here, logging.path and logging.file are configured, only logging.file will take effect, so if you want to specify the specific location of log generation, use logging.file configuration .

2) Configure in application.properties

	logging.file =F:\\log\\enjoy.log

Insert picture description here

			这样在F盘的相应位置出现日志文件。。

Two, log4j2

Searching for spring-boot-starter-log4j2 in spring-boot-dependencies POMs
found that Spring boot parent Pom provides this dependency, so we add the following jar dependency:

  1. Modify the pom.xml file
 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j2</artifactId>
        </dependency>
	注意: 由于默认使用logback在扩展log4j2之前先要把logback移除
	
	日志使用跟上面logback一样。

**Next chapter:** is being updated. . .

Guess you like

Origin blog.csdn.net/weixin_46822085/article/details/109353169