The default logging framework used by SpringBoot is logback. Spring boot starter contains the spring boot starter logging module. The log framework is logback. Therefore, we do not need to introduce the spring boot starter logging module separately.
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </dependency>
SpringBoot default log format
Format description
- Timestamp, accurate to milliseconds: 2021-11-13 14:35:20.298
- logback log level The log levels are: TRACE, DEBUG, INFO, WARN, ERROR, FATAL:INFO
- Process ID:1414
- Separator: the default is:---
- Thread Name: [restartedMain]
Case demonstration
We can use @ Slf4j annotation here.
pom.xml configuration
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.5.6</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.rumenz</groupId> <artifactId>lession10</artifactId> <version>0.0.1-SNAPSHOT</version> <name>lession10</name> <description>Section 10:SpringBoot Log management in</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.12</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
Print our log in the Controller
package com.rumenz.lession10.controller; import lombok.extern.slf4j.Slf4j; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * @className: RumenzController * @description: TODO Class description * @author: Entry station rumenz com * @date: 2021/11/11 **/ @Slf4j //Comments provided by lombok @RestController @RequestMapping("/rumenz") public class RumenzController { //If you don't use lombok, you need to use the following code to get the log operation object //Logger log = LoggerFactory.getLogger(RumenzController.class); @GetMapping("/index") public String index(){ log.trace("Trace journal..."); log.debug("Debug journal..."); log.info("Info journal..."); log.warn("Warn journal..."); log.error("Error journal..."); return "Entry station"; } }
Start the Lession10Application and visit the browser http://127.0.0.1:8080/rumenz/index View console
By default, logback prints the log to the console.
Output logback log to file
By default, SpringBoot will only output the log to the console and will not record it to a file. In the production environment, the log needs to be recorded to a file for archiving. If you need to output to a file, you can do so in application Set logging. In the properties configuration file file. Path or logging file. name
- logging. file. Path (the lower version is logging.path) sets the storage path of the log, which will generate spring. Path in the set directory Log file.
- logging. file. Name (the lower version is logging.name) sets the log file path and log file name, which can be absolute path or relative path.
The two cannot be used at the same time. If they are configured at the same time, only logging file. Name takes effect. By default, when the log size reaches 10M, it will be truncated and a new log file will be generated.
application.properties configuration
logging.file.name=rumenz.log
Start the Lession10Application and visit the browser http://127.0.0.1:8080/rumenz/index View rumenz.com in the current directory Log will find the contents of the log. Of course, the console will also output.
How to set the log level
The log level is provided by TRACE < DEBUG < INFO < warn < error < fatal level by level. If the log is set to INFO, the logs of DEBUG and TRACE levels will not be output.
The default log level of SpringBoot is INFO. We change the default log level of SpringBoot to WARN
application.properties
logging.level.root=WARN
Start the Lession10Application and look at the console
root is all log levels of the project. The startup succeeded, but there was no log output. We set the default log level to WARN. According to the hierarchical relationship, the logs of info, debug and trace are not output.
We are in application Properties change the log level of root to INFO, and the log level of business package written by yourself to DEBUG
logging.level.root=INFO //The root log outputs information at the INFO level logging.level.com.rumenz.lession10.controller.config=WARN //Specify the classes under the config package to output at the WARN level
We output an INFO level log in the RumenzConfig configuration class.
@Slf4j @Configuration public class RumenzConfig { @Bean public User user(){ log.info("This is info Level log"); return new User(1, "rumenz.com"); } } @Data @AllArgsConstructor class User{ private Integer id; private String name; }
Start the Lession10Application, look at the console and find the log Info ("this is the info level log"); No output.
Set log level in batch
If we want to give it to com rumenz. lession10. Controller and com rumenz. lession10. controller. Service sets a unified log level.
application. For customer configuration in properties, packages are separated by English commas
logging.group.rumenz=com.rumenz.lession10.controller,com.rumenz.lession10.controller.service logging.level.rumenz=INFO
Other configuration items of log
- logging.config log configuration;
- logging. logback. rollingpolicy. Max file size (logging. File. Max size for lower versions) maximum log file size;
- logging. logback. rollingpolicy. Max history (lower version logging. File. Max History) maximum number of archived files;
- logging. pattern. Log mode of console output;
- logging.pattern.dateformat the date format of the log;
- logging.pattern.file uses log mode by default
- logging.pattern.level log level
logging.pattern.console
Used to specify the format of console log output. application.properties can be configured
logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss} [%thread] %-6level %logger- %msg%n
logging.pattern.file
Used to specify the format of log output in the file. application.properties can be configured
logging.pattern.file=%d{yyyy-MM-dd HH:mm:ss} [%thread] %-6level %logger- %msg%n
Meanings of the above symbol parameters
- %D {yyyy MM DD HH: mm: SS} - log output time
- %thread -- the name of the process that outputs the log, which is very useful in Web applications and asynchronous task processing
- %-6level -- log level, with 5 characters aligned to the left
- %logger - the name of the log exporter
- %msg -- log message
- %n -- newline character of platform
Source code address of this summary:
- GitHub:https://github.com/mifunc/spr...
- Gitee:https://gitee.com/rumenz/spri...
- https://rumenz.com/rumenbiji/...
introduce
- My blog https://rumenz.com/ ,
- My toolbox https://tooltt.com/
- WeChat official account: [entry station]
- Follow the [entry station] and reply to [1001] to get the quick reference manual of common linux commands
- Follow the [entry station] reply [1003] to get the solution of LeetCode [java language implementation]
- Pay attention to [entry station] and reply to [1004] to get the summary of Java basic core
- Follow [entry site] and reply to [1009] to obtain Alibaba Java development manual