Springboot log integration Logback
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
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
Baidu Encyclopedia explained https://baike.baidu.com/item/slf4j/6408868
slf4j is not a specific log solution. It is a bit similar to jdbc. It uses a facade mode. It is an abstract implementation for all types of logs. 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 slf4j and the specific implementation of logback's default support
Two, edit the controller
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
private final Logger logger=LoggerFactory.getLogger(UserController.class);
Enter: localhost:8080/hello on the browser, you can see the output of the console log
Three, log level control
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 debug=true in src/main/resources/application.properties
- Configure logging.level.* to specifically output the log levels of which packages.
For example:
logging.level.root=INFO #Except the
following package is debug, all other files are info level
logging.level.org.springframework.web=DEBUG
logging.level.cn.enjoy.controller=DEBUG
Four, log files
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.
Configure logging.file =F:\log\enjoy.log in application.properties
In this way, the log file appears in the corresponding position of the F drive