今天介绍一下SpringBoot配置日志文件
SpringBoot在所有的内部日志中使用Commons Logging,但是默认配置也提供了对常用日志的支持,如Java Util Logging,Log4J,Log4J2和Logback。但是每种Logger都可以通过配置使用控制台或者文件输出日志内容。
一.SpringBoot默认日志Logback
SLF4J,是一个针对各类Java日志框架的统一Façade抽象。Java有很多的日志框架,如java.util.logging,log4j,logback,commons-logging,Spring框架使用的是Jakarta Commons Logging API(JCL)。而SLF4J定义了统一的日志抽象接口,而真正的日志实现则是在运行决定的。
Logback是log4j框架的作者开发的新一代日志框架,它能够适应诸多运行环境,支持SLF4J。
默认情况下,SpringBoot使用SLF4J+Logback记录日志。
二.日志输出的内容元素
日志输出的内容元素具体如下:
时间日期:精确到毫秒
日志级别:ERROR,WARN,INFO,DEBUG,TRACE
进程ID
分隔符:---标识实际日志的开始
线程名:方括号括起来的内容
Logger名:通常为源代码类名
日志内容
三.添加日志依赖
网上给出的日志依赖为:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </dependency>
Spring boot应用将自动使用logback作为应用日志框架,Spring Boot启动的时候,由org.springframework.boot.logging.Logging-Application-Listener根据情况初始化并使用。spring-boot-starter中包含spring-boot-starter-logging,该依赖内容就是Spring boot默认的日志框架logback。
我自己使用的依赖如下:
<dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.20</version> </dependency> <!-- 添加logback-classic依赖 --> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> </dependency> <!-- 添加logback-core依赖 --> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> </dependency>
四.控制台输出
日志级别从低到高分别是TRACE<DEBUG<INFO<WARN<ERROR<FATAL,如果设置为WARN,则低于WARN的信息都不会被输出。
SpringBoot中默认配置ERROR、WARN和INFO级别的日志输出到控制台。
在application.properties中配置logging.level.包名=‘日志级别’,可以控制控制台输出日志级别,举个例子:
首先,建一个springboot项目,项目目录如下:
第二步,配置pom.xml文件,加入jar包
pom.xml
<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 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>springboot_002</groupId> <artifactId>springboot_002</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>springboot_002 Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.20</version> </dependency> <!-- 添加logback-classic依赖 --> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> </dependency> <!-- 添加logback-core依赖 --> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> <!-- 继承父包 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.3.RELEASE</version> </parent> <build> <finalName>springboot_002</finalName> </build> </project>
第三步,在src/main/java目录下建包com.zk.myspringboot,并创建一个SpringBootApplicationFirst.java启动类。
SpringBootApplicationFirst.java
package com.zk.myspringboot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.support.SpringBootServletInitializer; @SpringBootApplication public class SpringBootApplicationFirst extends SpringBootServletInitializer{ public static void main(String[]args){ SpringApplication.run(SpringBootApplicationFirst.class, args); } }
第四步,在src/main/resources目录下配置application.properties文件,在application.properties中配置日志等级,如下:
logging.level.com.zk=debug
这里的日志级别等级可以更改。
最后,我们在src/test/java下创建包com.zk.myspringboot(测试类包名需要与启动类包名保持一致),并在包中创建一个测试类SpringBootApplicationTest.java。
SpringBootApplicationTest.java
package com.zk.myspringboot; 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; @RunWith(SpringRunner.class) @SpringBootTest public class SpringBootApplicationTests { //日志的级别 /* * 日志的级别是由高到低的 * SpringBoot默认日志级别---info级别 * trace>debug>info>warn>error */ //记录器 Logger logger=LoggerFactory.getLogger(getClass()); @Test public void contextLoads() { logger.trace("这是trace日志..."); logger.debug("这是debug日志..."); logger.info("这是info日志..."); logger.warn("这是warn日志..."); logger.error("这是error日志..."); } }
我们启动测试类,执行结果如下:
可以看到日志的输出内容。
当然我们可以通过配置属性文件中的logging.file或者logging.path将日志文件输出到单独的文件中,需要注意的的是,这两个配置文件的属性值不可同时使用,否则
只有logging.file生效。
一.如果我们配置
logging.file=SpringBoot.log
则在工程目录下会生成一个SpringBoot.log文件。
日志文件内容如下:
2020-04-08 16:38:31.006 INFO 20008 --- [main] c.z.m.SpringBootApplicationTests : Starting SpringBootApplicationTests on zhangkun0400 with PID 20008 (started by zhangkun04 in D:\eclipseworkspace\springboot_002) 2020-04-08 16:38:31.006 DEBUG 20008 --- [main] c.z.m.SpringBootApplicationTests : Running with Spring Boot v1.4.3.RELEASE, Spring v4.3.5.RELEASE 2020-04-08 16:38:31.007 INFO 20008 --- [main] c.z.m.SpringBootApplicationTests : No active profile set, falling back to default profiles: default 2020-04-08 16:38:31.036 INFO 20008 --- [main] o.s.w.c.s.GenericWebApplicationContext : Refreshing org.s[email protected]5d0a1059: startup date [Wed Apr 08 16:38:31 CST 2020]; root of context hierarchy 2020-04-08 16:38:32.802 INFO 20008 --- [main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.s[email protected]5d0a1059: startup date [Wed Apr 08 16:38:31 CST 2020]; root of context hierarchy 2020-04-08 16:38:32.842 INFO 20008 --- [main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest) 2020-04-08 16:38:32.844 INFO 20008 --- [main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse) 2020-04-08 16:38:32.870 INFO 20008 --- [main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2020-04-08 16:38:32.870 INFO 20008 --- [main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2020-04-08 16:38:32.901 INFO 20008 --- [main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2020-04-08 16:38:33.050 INFO 20008 --- [main] c.z.m.SpringBootApplicationTests : Started SpringBootApplicationTests in 2.379 seconds (JVM running for 3.06) 2020-04-08 16:38:33.073 DEBUG 20008 --- [main] c.z.m.SpringBootApplicationTests : 这是debug日志... 2020-04-08 16:38:33.073 INFO 20008 --- [main] c.z.m.SpringBootApplicationTests : 这是info日志... 2020-04-08 16:38:33.074 WARN 20008 --- [main] c.z.m.SpringBootApplicationTests : 这是warn日志... 2020-04-08 16:38:33.074 ERROR 20008 --- [main] c.z.m.SpringBootApplicationTests : 这是error日志... 2020-04-08 16:38:33.080 INFO 20008 --- [Thread-1] o.s.w.c.s.GenericWebApplicationContext : Closing org.s[email protected]5d0a1059: startup date [Wed Apr 08 16:38:31 CST 2020]; root of context hierarchy 2020-04-08 16:46:06.593 INFO 14984 --- [main] c.z.m.SpringBootApplicationTests : Starting SpringBootApplicationTests on zhangkun0400 with PID 14984 (started by zhangkun04 in D:\eclipseworkspace\springboot_002) 2020-04-08 16:46:06.594 DEBUG 14984 --- [main] c.z.m.SpringBootApplicationTests : Running with Spring Boot v1.4.3.RELEASE, Spring v4.3.5.RELEASE 2020-04-08 16:46:06.594 INFO 14984 --- [main] c.z.m.SpringBootApplicationTests : No active profile set, falling back to default profiles: default 2020-04-08 16:46:06.623 INFO 14984 --- [main] o.s.w.c.s.GenericWebApplicationContext : Refreshing org.s[email protected]5d0a1059: startup date [Wed Apr 08 16:46:06 CST 2020]; root of context hierarchy 2020-04-08 16:46:07.968 INFO 14984 --- [main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.s[email protected]5d0a1059: startup date [Wed Apr 08 16:46:06 CST 2020]; root of context hierarchy 2020-04-08 16:46:08.052 INFO 14984 --- [main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest) 2020-04-08 16:46:08.053 INFO 14984 --- [main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse) 2020-04-08 16:46:08.101 INFO 14984 --- [main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2020-04-08 16:46:08.102 INFO 14984 --- [main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2020-04-08 16:46:08.157 INFO 14984 --- [main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2020-04-08 16:46:08.410 INFO 14984 --- [main] c.z.m.SpringBootApplicationTests : Started SpringBootApplicationTests in 2.123 seconds (JVM running for 2.649) 2020-04-08 16:46:08.449 DEBUG 14984 --- [main] c.z.m.SpringBootApplicationTests : 这是debug日志... 2020-04-08 16:46:08.450 INFO 14984 --- [main] c.z.m.SpringBootApplicationTests : 这是info日志... 2020-04-08 16:46:08.450 WARN 14984 --- [main] c.z.m.SpringBootApplicationTests : 这是warn日志... 2020-04-08 16:46:08.451 ERROR 14984 --- [main] c.z.m.SpringBootApplicationTests : 这是error日志... 2020-04-08 16:46:08.460 INFO 14984 --- [Thread-1] o.s.w.c.s.GenericWebApplicationContext : Closing org.s[email protected]5d0a1059: startup date [Wed Apr 08 16:46:06 CST 2020]; root of context hierarchy 2020-04-08 21:07:59.766 INFO 15932 --- [main] c.z.m.SpringBootApplicationTests : Starting SpringBootApplicationTests on zhangkun0400 with PID 15932 (started by zhangkun04 in D:\eclipseworkspace\springboot_002) 2020-04-08 21:07:59.768 DEBUG 15932 --- [main] c.z.m.SpringBootApplicationTests : Running with Spring Boot v1.4.3.RELEASE, Spring v4.3.5.RELEASE 2020-04-08 21:07:59.769 INFO 15932 --- [main] c.z.m.SpringBootApplicationTests : No active profile set, falling back to default profiles: default 2020-04-08 21:07:59.814 INFO 15932 --- [main] o.s.w.c.s.GenericWebApplicationContext : Refreshing org.s[email protected]5d0a1059: startup date [Wed Apr 08 21:07:59 CST 2020]; root of context hierarchy 2020-04-08 21:08:01.360 INFO 15932 --- [main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.s[email protected]5d0a1059: startup date [Wed Apr 08 21:07:59 CST 2020]; root of context hierarchy 2020-04-08 21:08:01.418 INFO 15932 --- [main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest) 2020-04-08 21:08:01.420 INFO 15932 --- [main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse) 2020-04-08 21:08:01.447 INFO 15932 --- [main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2020-04-08 21:08:01.448 INFO 15932 --- [main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2020-04-08 21:08:01.487 INFO 15932 --- [main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2020-04-08 21:08:01.639 INFO 15932 --- [main] c.z.m.SpringBootApplicationTests : Started SpringBootApplicationTests in 2.275 seconds (JVM running for 3.022) 2020-04-08 21:08:01.661 DEBUG 15932 --- [main] c.z.m.SpringBootApplicationTests : 这是debug日志... 2020-04-08 21:08:01.661 INFO 15932 --- [main] c.z.m.SpringBootApplicationTests : 这是info日志... 2020-04-08 21:08:01.661 WARN 15932 --- [main] c.z.m.SpringBootApplicationTests : 这是warn日志... 2020-04-08 21:08:01.661 ERROR 15932 --- [main] c.z.m.SpringBootApplicationTests : 这是error日志... 2020-04-08 21:08:01.665 INFO 15932 --- [Thread-1] o.s.w.c.s.GenericWebApplicationContext : Closing org.s[email protected]5d0a1059: startup date [Wed Apr 08 21:07:59 CST 2020]; root of context hierarchy
二.如果我们配置的是loggin.path,如下:
logging.path=/spring/log
则会在我们项目的磁盘下创建一个log文件,
spring.log日志文件内容如下:
2020-04-08 16:51:48.278 INFO 16368 --- [main] c.z.m.SpringBootApplicationTests : Starting SpringBootApplicationTests on zhangkun0400 with PID 16368 (started by zhangkun04 in D:\eclipseworkspace\springboot_002) 2020-04-08 16:51:48.279 DEBUG 16368 --- [main] c.z.m.SpringBootApplicationTests : Running with Spring Boot v1.4.3.RELEASE, Spring v4.3.5.RELEASE 2020-04-08 16:51:48.280 INFO 16368 --- [main] c.z.m.SpringBootApplicationTests : No active profile set, falling back to default profiles: default 2020-04-08 16:51:48.323 INFO 16368 --- [main] o.s.w.c.s.GenericWebApplicationContext : Refreshing org.s[email protected]5d0a1059: startup date [Wed Apr 08 16:51:48 CST 2020]; root of context hierarchy 2020-04-08 16:51:51.273 INFO 16368 --- [main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.s[email protected]5d0a1059: startup date [Wed Apr 08 16:51:48 CST 2020]; root of context hierarchy 2020-04-08 16:51:51.402 INFO 16368 --- [main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest) 2020-04-08 16:51:51.404 INFO 16368 --- [main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse) 2020-04-08 16:51:51.469 INFO 16368 --- [main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2020-04-08 16:51:51.470 INFO 16368 --- [main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2020-04-08 16:51:51.560 INFO 16368 --- [main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2020-04-08 16:51:51.936 INFO 16368 --- [main] c.z.m.SpringBootApplicationTests : Started SpringBootApplicationTests in 4.249 seconds (JVM running for 5.196) 2020-04-08 16:51:51.996 DEBUG 16368 --- [main] c.z.m.SpringBootApplicationTests : 这是debug日志... 2020-04-08 16:51:51.997 INFO 16368 --- [main] c.z.m.SpringBootApplicationTests : 这是info日志... 2020-04-08 16:51:51.998 WARN 16368 --- [main] c.z.m.SpringBootApplicationTests : 这是warn日志... 2020-04-08 16:51:51.999 ERROR 16368 --- [main] c.z.m.SpringBootApplicationTests : 这是error日志... 2020-04-08 16:51:52.038 INFO 16368 --- [Thread-1] o.s.w.c.s.GenericWebApplicationContext : Closing org.s[email protected]5d0a1059: startup date [Wed Apr 08 16:51:48 CST 2020]; root of context hierarchy
参考网址如下:https://blog.csdn.net/qq_37859539/article/details/82464745
原文地址:https://www.cnblogs.com/longlyseul/p/12663155.html