Spring Boot默认情况下,当使用"Starters" 使用Logback输出日志

Spring Boot默认配置只会输出到控制台,并不会记录到文件中,但是我们通常生产环境使用时都需要以文件方式记录。

日志配置参数

logging:
    file:   # 日志文件,绝对路径或相对路径
    path:   # 保存日志文件目录路径
    config: # 日志配置文件,Spring Boot默认使用classpath路径下的日志配置文件,如:logback.xml
    level:  # 日志级别
        org.springframework.web: DEBUG # 配置spring web日志级别

PS:Spring Boot中的logging.path和logging.file这2个属性,只需要配置其中之一即可,如果同时配置,则使用logging.file属性。

配置文件目录

logging:
  file: C:\\logs\\springboot\\gfzs_smart.log
  level: 
    root: info

测试

import java.util.Date;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import com.fasterxml.jackson.databind.ObjectMapper;

@RunWith(SpringRunner.class)
@SpringBootTest
public class LoggerTest {
	
	protected final Logger logger = LoggerFactory.getLogger(this.getClass());  
    protected final ObjectMapper objectMapper = new ObjectMapper();  
      
    @Test  
    public void contextLoads() {  
        logger.trace("I am trace log.");  
        logger.debug("I am debug log.");  
        logger.info("I am info log.");  
        logger.warn("I am warn log.");  
        logger.error("I am error log.{}", new Date());  
    }  
}

更多推荐

spring boot日志配置 Logback、yml