跟我学SpringBoot之Logging

只需低头努力,剩下的交给时光,时间会公平地帮你处理一切

前言

log配置可能是被忽视的一个环节,一般的项目中日志配置好了基本上很少去改动,我们常规操作是log.info来记录日志内容,很少会有人注意到springBoot中日志的配置

日志格式

2021-03-09 21:33:06.594  INFO 21236 --- [           main] com.gzb.springboot.logging.LoggingMain   : Starting LoggingMain using Java 1.8.0_111 on JT-DS998-pzj with PID 21236 (E:\workspace\springBoot-demo\out\production\classes started by arno.peng in E:\workspace\springBoot-demo)
2021-03-09 21:33:06.597  INFO 21236 --- [           main] com.gzb.springboot.logging.LoggingMain   : The following profiles are active: dev
2021-03-09 21:33:08.012  INFO 21236 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2021-03-09 21:33:08.024  INFO 21236 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2021-03-09 21:33:08.024  INFO 21236 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.43]
2021-03-09 21:33:08.156  INFO 21236 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2021-03-09 21:33:08.156  INFO 21236 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1498 ms
2021-03-09 21:33:08.408  INFO 21236 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2021-03-09 21:33:08.643  INFO 21236 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2021-03-09 21:33:08.662  INFO 21236 --- [           main] com.gzb.springboot.logging.LoggingMain   : Started LoggingMain in 3.163 seconds (JVM running for 5.555)

如上图所示是springBoot中默认的日志输出格式,一般包括几个部分

  • 日期和时间:精确到毫秒,易于排序
  • 日志级别:ERROR, WARN, INFO, DEBUG, TRACE
  • 进程ID
  • [ main]表示线程名称
  • Logger名称:一般是类的路径
  • 最后是日志的内容

日志输出

控制台输出

默认情况下SpringBoot将日志输出到控制台,会输出INFO、WARN、ERROR这几个级别的日志,当然还可以通过DEBUG参数来输出DEBUG日志,比如你在application.yml配置中打开DEBUG日志

debug:true

或者以jar包的形式启动 java -jar app.jar --debug

都可以以DEBUG模式来启动项目,此时日志里面会多出很多信息

2021-03-09 21:48:22.581  INFO 2736 --- [           main] com.gzb.springboot.logging.LoggingMain   : Starting LoggingMain using Java 1.8.0_111 on JT-DS998-pzj with PID 2736 (E:\workspace\springBoot-demo\out\production\classes started by arno.peng in E:\workspace\springBoot-demo)
2021-03-09 21:48:22.586  INFO 2736 --- [           main] com.gzb.springboot.logging.LoggingMain   : The following profiles are active: dev
2021-03-09 21:48:22.590 DEBUG 2736 --- [           main] o.s.boot.SpringApplication               : Loading source class com.gzb.springboot.logging.LoggingMain
2021-03-09 21:48:22.681 DEBUG 2736 --- [           main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@a3d8174
2021-03-09 21:48:24.179 DEBUG 2736 --- [           main] .s.b.w.e.t.TomcatServletWebServerFactory : Code archive: D:\maven\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot\2.4.3\de2bd17a8eb9bc3dfa629aa06f2e9fe3bf603c85\spring-boot-2.4.3.jar
2021-03-09 21:48:24.180 DEBUG 2736 --- [           main] .s.b.w.e.t.TomcatServletWebServerFactory : Code archive: D:\maven\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot\2.4.3\de2bd17a8eb9bc3dfa629aa06f2e9fe3bf603c85\spring-boot-2.4.3.jar
2021-03-09 21:48:24.180 DEBUG 2736 --- [           main] .s.b.w.e.t.TomcatServletWebServerFactory : None of the document roots [src/main/webapp, public, static] point to a directory and will be ignored.
2021-03-09 21:48:24.205  INFO 2736 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2021-03-09 21:48:24.218  INFO 2736 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2021-03-09 21:48:24.218  INFO 2736 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.43]
2021-03-09 21:48:24.344  INFO 2736 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2021-03-09 21:48:24.344 DEBUG 2736 --- [           main] w.s.c.ServletWebServerApplicationContext : Published root WebApplicationContext as ServletContext attribute with name [org.springframework.web.context.WebApplicationContext.ROOT]
2021-03-09 21:48:24.344  INFO 2736 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1663 ms
2021-03-09 21:48:24.364 DEBUG 2736 --- [           main] o.s.b.w.s.ServletContextInitializerBeans : Mapping filters: characterEncodingFilter urls=[/*] order=-2147483648, formContentFilter urls=[/*] order=-9900, requestContextFilter urls=[/*] order=-105
2021-03-09 21:48:24.364 DEBUG 2736 --- [           main] o.s.b.w.s.ServletContextInitializerBeans : Mapping servlets: dispatcherServlet urls=[/]
2021-03-09 21:48:24.432 DEBUG 2736 --- [           main] o.s.b.w.s.f.OrderedRequestContextFilter  : Filter 'requestContextFilter' configured for use
2021-03-09 21:48:24.433 DEBUG 2736 --- [           main] s.b.w.s.f.OrderedCharacterEncodingFilter : Filter 'characterEncodingFilter' configured for use
2021-03-09 21:48:24.433 DEBUG 2736 --- [           main] o.s.b.w.s.f.OrderedFormContentFilter     : Filter 'formContentFilter' configured for use
2021-03-09 21:48:24.754  INFO 2736 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2021-03-09 21:48:24.772 DEBUG 2736 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : ControllerAdvice beans: 0 @ModelAttribute, 0 @InitBinder, 1 RequestBodyAdvice, 1 ResponseBodyAdvice
2021-03-09 21:48:24.877 DEBUG 2736 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : 2 mappings in 'requestMappingHandlerMapping'
2021-03-09 21:48:24.914 DEBUG 2736 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Patterns [/webjars/**, /resources/**] in 'resourceHandlerMapping'
2021-03-09 21:48:24.925 DEBUG 2736 --- [           main] .m.m.a.ExceptionHandlerExceptionResolver : ControllerAdvice beans: 0 @ExceptionHandler, 1 ResponseBodyAdvice
2021-03-09 21:48:24.943 DEBUG 2736 --- [           main] inMXBeanRegistrar$SpringApplicationAdmin : Application Admin MBean registered with name 'org.springframework.boot:type=Admin,name=SpringApplication'
2021-03-09 21:48:25.029  INFO 2736 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''

这个只是一部分,后面还有很多SpringBoot自动配置的信息因为篇幅的原因,没有贴上来
DEBUG模式适用于调试问题,打印更加详细的信息方便问题的定位

文件输出

默认情况下,SpringBoot会把日志输出到控制台,一般生产环境会将日志输出到文件,便于存储。如果想把日志写入文件,可以在application.yml配置文件中新增logging.file.name或者logging.file.path配置

  • logging.file.name 写入指定的日志文件,名称可以是绝对路径或相对路径
  • logging.file.path 写入指定的目录,日志名称是spring.log,路径可以是绝对路径或相对路径

日志级别

SpringBoot支持的所有日志系统都可以通过在application.yml中设置logging.level来配置日志级别,日志级别有TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF这几种,使用方式如下

logging:
  level:
    root: "warn"
    org.springframework.web: "debug"
    org.hibernate: "error"

以上可以为不同的功能模块设置不同的日志级别,例如将root设置为warn,将hibernate设置为error

日志分组

有时候我们需要对多个功能同时进行日志级别配置,这个时候可以利用日志的分组功能。例如,我们需要设置tomcat相关的日志级别,我们可以先将对应的模块分组

logging:
  group:
    tomcat: "org.apache.catalina,org.apache.coyote,org.apache.tomcat"

然后对分组进行日志级别的设置

logging:
  level:
    tomcat: "trace"

自定义日志配置

你可以在项目的类路径上包含适当的日志jar来激活对应的日志记录系统,也可以通过org.springframework.boot.logging.LoggingSystem系统属性来强制指定SpringBoot来使用指定的日志记录系统,你还可以使用none值完全禁用Spring Boot的日志记录配置

根据日志系统的不同,加载的文件也不同

Logging SystemCustomization
Logbacklogback-spring.xml, logback-spring.groovy, logback.xml, or logback.groovy
Log4j2log4j2-spring.xml or log4j2.xml
JDK (Java Util Logging)logging.properties

建议在日志配置中使用-spring变量(例如,logback-spring.xml而不是logback.xml)。
如果使用标准配置位置,Spring不能完全控制日志初始化。

为了方便个性化配置,一些spring日志配置转换成了系统配置

在这里插入图片描述

所有支持的日志记录系统都支持系统属性的配置,说白了就是为了简化配置,将logging配置转换成系统属性

具体详细配置可以参考SpringBoot官方文档中各个日志系统的用法

Logback扩展

Spring Boot包括许多对Logback的扩展,可以帮助进行高级配置。您可以在配置文件logback-spring.xml中使用这些扩展。

由于标准的logback.xml配置文件加载得太早,因此不能在其中使用扩展。您需要使用logback-spring.xml或定义logging.config属性

概要文件配置

<springProfile name="staging">
    <!-- "staging" profile 被激活时启用此配置 -->
</springProfile>

<springProfile name="dev | staging">
    <!-- "dev" or "staging"  profile 被激活时启用此配置 -->
</springProfile>

<springProfile name="!production">
    <!-- 当不是"production"被激活时,启用此配置 -->
</springProfile>

以上标签可以让你根据spring的profile激活的配置来选择性的进行日志配置,name属性用于指定激活的spring profile,可以正向指定,也可以用排除的方式

环境配置

<springProperty scope="context" name="fluentHost" source="myapp.fluentd.host"
        defaultValue="localhost"/>
<appender name="FLUENT" class="ch.qos.logback.more.appenders.DataFluentAppender">
    <remoteHost>${fluentHost}</remoteHost>
    ...
</appender>

标签用于将spring上下文中的属性公布出来,可以在logback中使用,如上所示,
定义了参数fluentHost,默认值为localhost,可以直接在配置文件中以${fluentHost}形式引用

好了,日志的部分就这么多,没有写太多的例子,大概了解一下后可以根据自己系统的实际情况来进行自定义配置,或者就使用springBoot默认的日志配置也没太多的问题

如果感觉对你有些帮忙,请收藏好,你的关注和点赞是对我最大的鼓励!
如果想跟我一起学习,坚信技术改变世界,请关注【Java天堂】公众号,我会定期分享自己的学习成果,第一时间推送给您

在这里插入图片描述

posted @ 2021-03-09 23:20  果子爸聊技术  阅读(14)  评论(0编辑  收藏  举报