知识点1,默认开启日志系统,info级别
springboot starter中默认支持log4j2,logback
这里引入slf4j相关的logger包
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class StudyRestApplicationTests {
private Logger logger = LoggerFactory.getLogger(StudyRestApplicationTests5.class);
@Test
public void testLog() {
logger.trace("trace***");
logger.debug("debug***");
logger.info("info***");
logger.warn("warn***");
logger.error("error***");
}
}
打印结果
2021-01-13 23:11:15.130 INFO 19536 --- [ main] c.e.s.StudyRestApplicationTests5 : info***
2021-01-13 23:11:15.131 WARN 19536 --- [ main] c.e.s.StudyRestApplicationTests5 : warn***
2021-01-13 23:11:15.131 ERROR 19536 --- [ main] c.e.s.StudyRestApplicationTests5 : error***
知识点2,日志级别设置
logging.level.项目包名=日志级别
,举例
logging.level.com.example.studyRest=warn
打印结果
2021-01-13 23:14:55.478 WARN 20836 --- [ main] c.e.s.StudyRestApplicationTests5 : warn***
2021-01-13 23:14:55.478 ERROR 20836 --- [ main] c.e.s.StudyRestApplicationTests5 : error***
知识点3,将日志写到文件夹下
logging.file.path=D:/log
默认生成文件:spring.log
知识点4,将日志写到指定文件中
ogging.file.name=D:/log/runtime.log
知识点5,日志格式
logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss.SSS};my-app;%msg%n
logging.pattern.file=%d{yyyy-MM-dd HH:mm:ss.SSS};my-app;%msg%n
详细pattern日志格式参考:https://blog.csdn.net/dengjili/article/details/98369441
springboot 官网Logging使用
https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-logging