Spring Boot Personal Learning Notes #2

8. log

Common log frameworks:

JUL JCL jboos-logging logback log4j log4j2 slf4j...

Log facade (log abstraction layer) Log implementation
JCL(jakarta commons logging) , slf4j(simple logging facade for java) , jboss-logging log4j , JUL(java.util.logging) , log4j2 , logback

Choose a facade on the left and an implementation on the right.

Log facade: slf4j

Log implementation: logback

Spring Boot: The bottom layer is the Spring framework, which defaults to JCL

Spring Boot uses slf4j and logback

How to use slf4j in the system

In future development, the call of log recording method should not directly call the implementation class of log, but call the method in the log abstraction layer.

Implementing jar and logback by importing slf4j into the system

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

public class HelloWorld {
  public static void main(String[] args) {
    Logger logger = LoggerFactory.getLogger(HelloWorld.class);
    logger.info("Hello World");
  }
}

Each log implementation framework has its own configuration file. After using slf4j, the configuration file is still the configuration file of the log implementation framework.

How to unify all logs in the system to slf4j

1. Exclude all other logging frameworks in the system

2. Replace the original log framework with a tundish

3. We import other implementations of slf4j

SpringBook log relationships

SpringBoot uses it for logging

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

Underlying dependencies

[Img-xYcdWzYd-1568 1928747 (C: Users Y Desktop Miscellaneous 1509522. png)]

summary

1. The SpringBook underlying layer is also recorded using slf4j + logback

2.SpringBoot also replaced all other logs with slf4j

3. The replacement package in the middle.

Log usage

1. Default configuration

SpringBoot helped us configure our logs

Use

@RunWith(SpringRunner.class)
@SpringBootTest
public class HelloworldApplicationTests {
	//Recorder
	Logger logger = LoggerFactory.getLogger(getClass());
	@Test
	public void contextLoads() {
		//Log level: from low to high trace < debug < info < warn < error
		//The log level from which output can be adjusted will take effect
		//SpringBoot takes effect by default from the info level
		logger.trace("trace...");
		logger.debug("debug...");
		logger.info("info...");
		logger.warn("warning...");
		logger.error("error...");
	}

output

2019-09-10 14:57:13.602  INFO 5644 --- [           main] c.e.h.HelloworldApplicationTests         : info...
2019-09-10 14:57:13.602  WARN 5644 --- [           main] c.e.h.HelloworldApplicationTests         : warning...
2019-09-10 14:57:13.602 ERROR 5644 --- [           main] c.e.h.HelloworldApplicationTests         : error...
2. Custom Configuration

We can adjust the log level of output in the configuration file

logging.level.com.example=trace

output

2019-09-10 15:02:51.088 TRACE 18160 --- [           main] c.e.h.HelloworldApplicationTests         : trace...
2019-09-10 15:02:51.088 DEBUG 18160 --- [           main] c.e.h.HelloworldApplicationTests         : debug...
2019-09-10 15:02:51.088  INFO 18160 --- [           main] c.e.h.HelloworldApplicationTests         : info...
2019-09-10 15:02:51.088  WARN 18160 --- [           main] c.e.h.HelloworldApplicationTests         : warning...
2019-09-10 15:02:51.088 ERROR 18160 --- [           main] c.e.h.HelloworldApplicationTests         : error...

Use of logging.file and logging.path (in configuration files)

logging.file logging.path example description
(none) (none) Output in console only
specify a filename (none) my.log Output log to my.log file
(none) Specified directory /var/log Output to the spring.log file of the specified directory

Example:

configuration file

logging.level.com.example=trace
logging.file=testLog.log

After running, the root directory has an additional testLog.log, which contains the log content (specifiable path)

configuration file

logging.level.com.example=trace
logging.path=/spring/log

After running, the spring.log file is generated under the spring/log folder of the current disk.

Note: Log. file and log. path conflict, and only one can be specified at the same time. If they are used at the same time, then log. file will prevail!

In addition, it can be used

Log. pattern. console (console)

Log. pattern. file (file)

Control the format of logs in consoles and files

3. Switching Log Framework

...

Keywords: Spring SpringBoot log4j Java

Added by danielle on Wed, 11 Sep 2019 12:15:00 +0300