在主配置类中添加:
Logger logger =LoggerFactory.getLogger(HelloworldApplicationTests.class); @Test public void test_log(){ logger.trace("trace******"); logger.debug("debug******"); logger.info("info******"); logger.warn("warn******"); logger.error("error******"); }
Spring boot默认的日志级别是INFO(即只打印 info及之后级别的信息):也可以自定义级别:
全局配置文件中logging.level.com.doublechen.helloworld=warn
在配置文件中写入logging.file.name=springboot.log,可以将日志信息输出到文件中。
指定日志显示格式:
a.在控制台中:
logging.pattern.console=%d{yyyy-MM-dd} [%thread] %-5level %logger{50} - %msg%n
%d:日期时间
%thread:线程名称
%-5level:显示日志级别,-5表示从左显示5个字符宽度
%logger{50}:设置日志长度
%msg:日志消息
%n:回车
b.在文件中:
logging.pattern.file=%d{yyyy-MM-dd} ** [%thread] ** %-5level ** %logger{50}** %msg%n