Spring Boot Logging 配置

前言

spring boot 使用 Commons Logging 作为内部的日志系统,并且给 Java Util Logging,Log4J2 以及 Logback 都提供了默认的配置。如果使用了 spring boot的 Starters,那么默认会使用 Logback 用于记录日志。日志默认输出到控制台但也能输出到文件中。

一、Log format

spring boot中默认的日志输出格式如下:

2021-04-13 14:26:14.872  INFO 12428 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2021-04-13 14:26:14.872  INFO 12428 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1379 ms
2021-04-13 14:26:14.962  INFO 12428 --- [           main] c.a.d.s.b.a.DruidDataSourceAutoConfigure : Init DruidDataSource
2021-04-13 14:26:15.114  INFO 12428 --- [           main] com.alibaba.druid.pool.DruidDataSource   : {dataSource-1} inited
2021-04-13 14:26:15.874  INFO 12428 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2021-04-13 14:26:16.101  INFO 12428 --- [           main] o.s.s.web.DefaultSecurityFilterChain     : Will secure any request with [org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@78d6447a, org.springframework.security.web.context.SecurityContextPersistenceFilter@2b2f5fcf, org.springframework.security.web.header.HeaderWriterFilter@75483843, org.springframework.security.web.authentication.logout.LogoutFilter@2b8bb184, org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter@2a3194c6, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@711d1a52, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@288ca5f0, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@7a729f84, org.springframework.security.web.session.SessionManagementFilter@5ec4ff02, org.springframework.security.web.access.ExceptionTranslationFilter@14ef2482, org.springframework.security.web.access.intercept.FilterSecurityInterceptor@1a4d1ab7]
2021-04-13 14:26:16.210  INFO 12428 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8081 (http) with context path ''
2021-04-13 14:26:16.219  INFO 12428 --- [           main] org.javaboy.vhr.VhrApplication           : Started VhrApplication in 3.166 seconds (JVM running for 4.234)
2021-04-13 14:26:31.579  INFO 12428 --- [nio-8081-exec-1] o.apache.tomcat.util.http.parser.Cookie  : A cookie header was received [1604626050,1606126871,1606434674; username-localhost-8888="2|1:0|10:1616408620|23:username-localhost-8888|44:YzZmNjk3ZDI3MjM2NDYxMWE1N2E3NTVmODc0YzllY2E=|bc1e0d3af6e35720077cbcf499e7cd01b7a72764efc7d767c4a8d72cbacacf22"; JSESSIONID=3F003816EE3D479B3D99E707441614B5] that contained an invalid cookie. That cookie will be ignored.

以下项将会被输出:

1、日期和时间--精确到毫秒,并按照时间进行简单的排序

2、日志级别--ERROR,WARN,INFO,DEBUG,TRACE

3、进程ID号

4、日志内容,用"---"分隔符分开

5、线程名字--括在方括号中

6、日志的名字--通常对应的是类名

注意:Logback没有FATAL级别(映射到ERROR)

二、控制台输出

默认的日志配置将在写入控制台时回显消息,默认会显示 ERROR,WARN,和 INFO 级别的消息,你同样可以在启动的时候,启用 debug 模式,启动命令如下:java -jar yourapp.jar --debug

注意:你同样可以在 application.propertie 配置文件中指定 debug=true,来开启 debug,模式

一旦开启了 debug 模式,那么控制台同时会输出容器信息,hibernate 信息和 spring boot 的信息。

三、文件输出

默认情况下,spring boot 只会将日志输出到 console,而不会输出到日志文件中,如果你想将日志写到日志文件中,那么需要在 application.properties 配置文件中设置 logging.file 或者是logging.path

注意:此处是或者的关系,也就是说,你配置 logging.file 或者是 logging.path,效果是一样的。

下面的表格将显示如何进行配置文件输出:

logging.file logging.path Example 说明
二者都不配置,则只输出到Console
指定文件 my.log 写入指定的日志文件,文件名可以是一个确切的位置或相对目录
指定的目录 /var/log 将日志文件写入指定的目录,目录可以是一个确切的位置或者是一个相对目录

默认情况下,如果日志文件的大小达到10Mb的话,就会被截断,输出到新的日志文件中。

四、日志级别

所有支持的日志系统都可以通过 Spring Environment 来指定日志级别,例如 application.properties,可以使用 “logging.level.*=LEVEL” 来指定日志的级别,"LEVEL" 的取值可以是 TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF。配置示例如下:

logging.level.root=WARN #root日志以WARN级别输出
logging.level.org.springframework.web=DEBUG #org.springframework.web包下的日志以DEBUG级别输出
logging.level.org.hibernate=ERROR #org.hibernate包下的日志以ERROR级别输出

如果,我们需要指定我们的应用日志级别了,我们也可以使用同样的方式,如下:

logging.level.com.chhliu=debug

上面配置中的"com.chhliu"为我们应用的包名。

logging.level 设置日志级别,后面跟生效的区域,比如 root 表示整个项目,也可以设置为某个包下,也可以具体到某个类名(日志级别的值不区分大小写)

五、自定义日志输出格式

我们可以通过 logging.pattern.file 以及 logging.pattern.level 来配置我们需要的日志输出格式,例如:

logging.pattern.console=%d{yyyy/MM/dd-HH:mm:ss} [%thread] %-5level %logger- %msg%n
logging.pattern.file=%d{yyyy/MM/dd-HH:mm} [%thread] %-5level %logger- %msg%n

注意:以上配置,只对 Logback 起作用

参考文章

https://blog.csdn.net/liuchuanhong1/article/details/65442302

https://www.jianshu.com/p/1fa12b92d5c4?utm_campaign=haruki

原文地址:https://www.cnblogs.com/youcoding/p/14653437.html