Spring Boot uses the default logging system to add dependency first
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</dependency>
Add a new configuration class, print the log in the method
package com.example.demo.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class LogConfig {
private static final Logger LOG = LoggerFactory.getLogger(LogConfig.class);
@Bean
public String logMethod() {
LOG.info("==========print log==========");
return "SayHi";
}
}
application.properties can write log related properties
logging.level.root log level
logging.pattern.console log output format
logging.path property is used to configure the path of the log file
Add print log in method
package com.example.demo.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.example.demo.model.Student;
@RestController
public class HelloController {
private static final Logger LOG = LoggerFactory.getLogger(HelloController.class);
@RequestMapping("/sayHi")
public String sayHi(@RequestBody Student demo) {
LOG.info("OK");
return demo.getName()+demo.getAge();
}
}
Run Spring boot like this to print out the log as follows