Like Share Discussion Bookmark Smile

J.J. Huang   2019-03-20   Spring Boot   瀏覽次數:次   DMCA.com Protection Status

SpringBoot - 第十章 | 日誌管理

Spring Boot使用Commons Logging進行所有內部日誌記錄,但保留底層日誌實現。 為Java Util Logging,Log4J2和Logback提供了預設配置。在每種情況下,記錄器都預先配置為使用控制台輸出,並且還提供可選的文件輸出。

預設情況下,如果使用”Starters”,則將使用Logback進行日誌記錄。 還包括適當的Logback路由,以確保使用Java Util Logging,Commons Logging,Log4J或SLF4J的依賴庫都能正常工作。


預設日誌

  • 預設情況下,Spring Boot會用Logback來記錄日誌,並用INFO級別輸出到控制台。在運行應用程序和其他例子時,你應該已經看到很多INFO級別的日誌了。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  .   ____          _            __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.3.RELEASE)

2019-03-19 09:38:22.003 INFO 2588 --- [ main] c.j.l.s.chapter10.Chapter10Application : Starting Chapter10Application on localhost with PID 2588 (/Users/morose/Documents/workspace-SpringBoot/chapter-10/target/classes started by morose in /Users/morose/Documents/workspace-SpringBoot/chapter-10)
2019-03-19 09:38:22.008 INFO 2588 --- [ main] c.j.l.s.chapter10.Chapter10Application : No active profile set, falling back to default profiles: default
2019-03-19 09:38:23.144 INFO 2588 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2019-03-19 09:38:23.175 INFO 2588 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2019-03-19 09:38:23.175 INFO 2588 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.16]
2019-03-19 09:38:23.187 INFO 2588 --- [ main] o.a.catalina.core.AprLifecycleListener : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/Users/morose/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.]
2019-03-19 09:38:23.295 INFO 2588 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2019-03-19 09:38:23.295 INFO 2588 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1212 ms
2019-03-19 09:38:23.554 INFO 2588 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2019-03-19 09:38:23.803 INFO 2588 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2019-03-19 09:38:23.806 INFO 2588 --- [ main] c.j.l.s.chapter10.Chapter10Application : Started Chapter10Application in 2.149 seconds (JVM running for 2.555)

格式化日誌

  • 針對其輸出內容元素做說明
訊息內容 元素 說明
2019-03-19 09:38:23.144 時間日期 精確到毫秒
INFO 日誌級別 ERROR, WARN, INFO, DEBUG or TRACE
2588 進程ID 進程ID
- - - 分隔符 - - - 標識實際日誌的開始
[ main] 線程名 方括號括起來(可能會截斷控制台輸出)
o.s.b.w.embedded.tomcat.TomcatWebServer Logger名 通常使用源代碼的類名
Tomcat initialized with port(s): 8080 (http) 日誌內容 日誌內容

控制台輸出

在Spring Boot中預設配置了FATAL、ERROR、WARN和INFO級別的日誌輸出到控制台。

  • 撰寫 LoggerDemoController

可以看到輸出的Log等級,就是包含INFO以上的等級

我們可以通過兩種方式切換至DEBUG級別:

  • 在運行命令後加入 –debug,如:$java -jar xxx.jar –debug

  • 在application.properties中 配置debug=true,該屬性置為true的時候,核心Logger(包含嵌入式容器、hibernate、spring)會輸出更多內容,但是你自己應用的日誌並不會輸出為DEBUG級別

  • 修改 application.properties

1
debug=true
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
2019-03-19 10:35:42.209 DEBUG 2722 --- [           main] .c.l.ClasspathLoggingApplicationListener : Application started with classpath: [file:/Users/morose/Documents/workspace-SpringBoot/chapter-10/target/classes/, file:/Users/morose/.m2/repository/org/springframework/boot/spring-boot-starter-web/2.1.3.RELEASE/spring-boot-starter-web-2.1.3.RELEASE.jar, file:/Users/morose/.m2/repository/org/springframework/boot/spring-boot-starter/2.1.3.RELEASE/spring-boot-starter-2.1.3.RELEASE.jar, file:/Users/morose/.m2/repository/org/springframework/boot/spring-boot/2.1.3.RELEASE/spring-boot-2.1.3.RELEASE.jar, file:/Users/morose/.m2/repository/org/springframework/boot/spring-boot-autoconfigure/2.1.3.RELEASE/spring-boot-autoconfigure-2.1.3.RELEASE.jar, file:/Users/morose/.m2/repository/org/springframework/boot/spring-boot-starter-logging/2.1.3.RELEASE/spring-boot-starter-logging-2.1.3.RELEASE.jar, file:/Users/morose/.m2/repository/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar, file:/Users/morose/.m2/repository/ch/qos/logback/logback-core/1.2.3/logback-core-1.2.3.jar, file:/Users/morose/.m2/repository/org/apache/logging/log4j/log4j-to-slf4j/2.11.2/log4j-to-slf4j-2.11.2.jar, file:/Users/morose/.m2/repository/org/apache/logging/log4j/log4j-api/2.11.2/log4j-api-2.11.2.jar, file:/Users/morose/.m2/repository/org/slf4j/jul-to-slf4j/1.7.25/jul-to-slf4j-1.7.25.jar, file:/Users/morose/.m2/repository/org/yaml/snakeyaml/1.23/snakeyaml-1.23.jar, file:/Users/morose/.m2/repository/org/springframework/boot/spring-boot-starter-json/2.1.3.RELEASE/spring-boot-starter-json-2.1.3.RELEASE.jar, file:/Users/morose/.m2/repository/com/fasterxml/jackson/core/jackson-databind/2.9.8/jackson-databind-2.9.8.jar, file:/Users/morose/.m2/repository/com/fasterxml/jackson/core/jackson-annotations/2.9.0/jackson-annotations-2.9.0.jar, file:/Users/morose/.m2/repository/com/fasterxml/jackson/core/jackson-core/2.9.8/jackson-core-2.9.8.jar, file:/Users/morose/.m2/repository/com/fasterxml/jackson/datatype/jackson-datatype-jdk8/2.9.8/jackson-datatype-jdk8-2.9.8.jar, file:/Users/morose/.m2/repository/com/fasterxml/jackson/datatype/jackson-datatype-jsr310/2.9.8/jackson-datatype-jsr310-2.9.8.jar, file:/Users/morose/.m2/repository/com/fasterxml/jackson/module/jackson-module-parameter-names/2.9.8/jackson-module-parameter-names-2.9.8.jar, file:/Users/morose/.m2/repository/org/hibernate/validator/hibernate-validator/6.0.14.Final/hibernate-validator-6.0.14.Final.jar, file:/Users/morose/.m2/repository/javax/validation/validation-api/2.0.1.Final/validation-api-2.0.1.Final.jar, file:/Users/morose/.m2/repository/org/jboss/logging/jboss-logging/3.3.2.Final/jboss-logging-3.3.2.Final.jar, file:/Users/morose/.m2/repository/com/fasterxml/classmate/1.4.0/classmate-1.4.0.jar, file:/Users/morose/.m2/repository/org/springframework/spring-web/5.1.5.RELEASE/spring-web-5.1.5.RELEASE.jar, file:/Users/morose/.m2/repository/org/springframework/spring-beans/5.1.5.RELEASE/spring-beans-5.1.5.RELEASE.jar, file:/Users/morose/.m2/repository/org/springframework/spring-webmvc/5.1.5.RELEASE/spring-webmvc-5.1.5.RELEASE.jar, file:/Users/morose/.m2/repository/org/springframework/spring-aop/5.1.5.RELEASE/spring-aop-5.1.5.RELEASE.jar, file:/Users/morose/.m2/repository/org/springframework/spring-context/5.1.5.RELEASE/spring-context-5.1.5.RELEASE.jar, file:/Users/morose/.m2/repository/org/springframework/spring-expression/5.1.5.RELEASE/spring-expression-5.1.5.RELEASE.jar, file:/Users/morose/.m2/repository/org/springframework/boot/spring-boot-starter-tomcat/2.1.3.RELEASE/spring-boot-starter-tomcat-2.1.3.RELEASE.jar, file:/Users/morose/.m2/repository/javax/annotation/javax.annotation-api/1.3.2/javax.annotation-api-1.3.2.jar, file:/Users/morose/.m2/repository/org/apache/tomcat/embed/tomcat-embed-core/9.0.16/tomcat-embed-core-9.0.16.jar, file:/Users/morose/.m2/repository/org/apache/tomcat/embed/tomcat-embed-el/9.0.16/tomcat-embed-el-9.0.16.jar, file:/Users/morose/.m2/repository/org/apache/tomcat/embed/tomcat-embed-websocket/9.0.16/tomcat-embed-websocket-9.0.16.jar, file:/Users/morose/.m2/repository/org/slf4j/slf4j-api/1.7.25/slf4j-api-1.7.25.jar, file:/Users/morose/.m2/repository/org/springframework/spring-core/5.1.5.RELEASE/spring-core-5.1.5.RELEASE.jar, file:/Users/morose/.m2/repository/org/springframework/spring-jcl/5.1.5.RELEASE/spring-jcl-5.1.5.RELEASE.jar]

. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.3.RELEASE)

2019-03-19 10:35:42.319 INFO 2722 --- [ main] c.j.l.s.chapter10.Chapter10Application : Starting Chapter10Application on localhost with PID 2722 (/Users/morose/Documents/workspace-SpringBoot/chapter-10/target/classes started by morose in /Users/morose/Documents/workspace-SpringBoot/chapter-10)
2019-03-19 10:35:42.320 INFO 2722 --- [ main] c.j.l.s.chapter10.Chapter10Application : No active profile set, falling back to default profiles: default
2019-03-19 10:35:42.320 DEBUG 2722 --- [ main] o.s.boot.SpringApplication : Loading source class com.jj.learning.springboot.chapter10.Chapter10Application
2019-03-19 10:35:42.389 DEBUG 2722 --- [ main] o.s.b.c.c.ConfigFileApplicationListener : Loaded config file 'file:/Users/morose/Documents/workspace-SpringBoot/chapter-10/target/classes/application.properties' (classpath:/application.properties)
2019-03-19 10:35:42.390 DEBUG 2722 --- [ main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@50a7bc6e
2019-03-19 10:35:43.400 DEBUG 2722 --- [ main] .s.b.w.e.t.TomcatServletWebServerFactory : Code archive: /Users/morose/.m2/repository/org/springframework/boot/spring-boot/2.1.3.RELEASE/spring-boot-2.1.3.RELEASE.jar
2019-03-19 10:35:43.401 DEBUG 2722 --- [ main] .s.b.w.e.t.TomcatServletWebServerFactory : Code archive: /Users/morose/.m2/repository/org/springframework/boot/spring-boot/2.1.3.RELEASE/spring-boot-2.1.3.RELEASE.jar
2019-03-19 10:35:43.401 DEBUG 2722 --- [ main] .s.b.w.e.t.TomcatServletWebServerFactory : Document root: /Users/morose/Documents/workspace-SpringBoot/chapter-10/src/main/webapp
2019-03-19 10:35:43.434 INFO 2722 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2019-03-19 10:35:43.467 INFO 2722 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2019-03-19 10:35:43.468 INFO 2722 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.16]
2019-03-19 10:35:43.483 INFO 2722 --- [ main] o.a.catalina.core.AprLifecycleListener : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/Users/morose/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.]
2019-03-19 10:35:43.602 INFO 2722 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2019-03-19 10:35:43.602 INFO 2722 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1212 ms
2019-03-19 10:35:43.635 DEBUG 2722 --- [ main] o.s.b.w.s.ServletContextInitializerBeans : Mapping filters: characterEncodingFilter urls=[/*], hiddenHttpMethodFilter urls=[/*], formContentFilter urls=[/*], requestContextFilter urls=[/*]
2019-03-19 10:35:43.636 DEBUG 2722 --- [ main] o.s.b.w.s.ServletContextInitializerBeans : Mapping servlets: dispatcherServlet urls=[/]
2019-03-19 10:35:43.664 DEBUG 2722 --- [ main] o.s.b.w.s.f.OrderedRequestContextFilter : Filter 'requestContextFilter' configured for use
2019-03-19 10:35:43.665 DEBUG 2722 --- [ main] .s.b.w.s.f.OrderedHiddenHttpMethodFilter : Filter 'hiddenHttpMethodFilter' configured for use
2019-03-19 10:35:43.665 DEBUG 2722 --- [ main] s.b.w.s.f.OrderedCharacterEncodingFilter : Filter 'characterEncodingFilter' configured for use
2019-03-19 10:35:43.665 DEBUG 2722 --- [ main] o.s.b.w.s.f.OrderedFormContentFilter : Filter 'formContentFilter' configured for use
2019-03-19 10:35:43.890 INFO 2722 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2019-03-19 10:35:44.111 DEBUG 2722 --- [ main] ConditionEvaluationReportLoggingListener :


============================
CONDITIONS EVALUATION REPORT
============================


Positive matches:
-----------------

CodecsAutoConfiguration matched:
- @ConditionalOnClass found required class 'org.springframework.http.codec.CodecConfigurer' (OnClassCondition)

CodecsAutoConfiguration.JacksonCodecConfiguration matched:
- @ConditionalOnClass found required class 'com.fasterxml.jackson.databind.ObjectMapper' (OnClassCondition)


省略...


Negative matches:
-----------------

ActiveMQAutoConfiguration:
Did not match:
- @ConditionalOnClass did not find required class 'javax.jms.ConnectionFactory' (OnClassCondition)

AopAutoConfiguration:
Did not match:
- @ConditionalOnClass did not find required class 'org.aspectj.lang.annotation.Aspect' (OnClassCondition)

省略...


Exclusions:
-----------

None


Unconditional classes:
----------------------

org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration

org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration

org.springframework.boot.autoconfigure.info.ProjectInfoAutoConfiguration



2019-03-19 10:35:44.160 INFO 2722 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2019-03-19 10:35:44.164 INFO 2722 --- [ main] c.j.l.s.chapter10.Chapter10Application : Started Chapter10Application in 2.179 seconds (JVM running for 2.558)
2019-03-19 10:36:02.294 INFO 2722 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2019-03-19 10:36:02.294 INFO 2722 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2019-03-19 10:36:02.300 INFO 2722 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 6 ms
2019-03-19 10:36:02.340 INFO 2722 --- [nio-8080-exec-1] c.j.l.s.c.c.LoggerDemoController : info(訊息):123
2019-03-19 10:36:02.342 WARN 2722 --- [nio-8080-exec-1] c.j.l.s.c.c.LoggerDemoController : warn(警告):123
2019-03-19 10:36:02.342 ERROR 2722 --- [nio-8080-exec-1] c.j.l.s.c.c.LoggerDemoController : error(錯誤):123
2019-03-19 10:36:02.342 ERROR 2722 --- [nio-8080-exec-1] c.j.l.s.c.c.LoggerDemoController : fatal(致命):123

可以看到多輸出了很多的資訊,但是在最後自己的應用Log依舊是INFO等級的,這就是「核心Logger(包含嵌入式容器、hibernate、spring)會輸出更多內容,但是你自己應用的日誌並不會輸出為DEBUG級別。」這句話的意思。


文件輸出

Spring Boot預設配置只會輸出到控制台,並不會記錄到文件中,但是我們通常生產環境使用時都需要以文件方式記錄。

若要增加文件輸出,需要在application.properties中配置logging.file或logging.path屬性。

  • logging.file,設置文件,可以是絕對路徑,也可以是相對路徑。如:logging.file=my.log
  • logging.path,設置目錄,會在該目錄下建立spring.log文件,並寫入日誌內容,如:logging.path=/var/log

註:二者不能同時使用,若同時使用,則只有logging.file生效,預設情況下,日誌文件的大小達到10MB時會截斷,產生新的日誌文件,預設級別為:ERROR、WARN、INFO

  • 修改 application.properties
1
logging.file=my.log
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2019-03-19 11:04:31.532  INFO 2807 --- [main] c.j.l.s.chapter10.Chapter10Application   : Starting Chapter10Application on localhost with PID 2807 (/Users/morose/Documents/workspace-SpringBoot/chapter-10/target/classes started by morose in /Users/morose/Documents/workspace-SpringBoot/chapter-10)
2019-03-19 11:04:31.536 INFO 2807 --- [main] c.j.l.s.chapter10.Chapter10Application : No active profile set, falling back to default profiles: default
2019-03-19 11:04:32.588 INFO 2807 --- [main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2019-03-19 11:04:32.625 INFO 2807 --- [main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2019-03-19 11:04:32.626 INFO 2807 --- [main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.16]
2019-03-19 11:04:32.638 INFO 2807 --- [main] o.a.catalina.core.AprLifecycleListener : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/Users/morose/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.]
2019-03-19 11:04:32.751 INFO 2807 --- [main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2019-03-19 11:04:32.751 INFO 2807 --- [main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1150 ms
2019-03-19 11:04:33.027 INFO 2807 --- [main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2019-03-19 11:04:33.281 INFO 2807 --- [main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2019-03-19 11:04:33.284 INFO 2807 --- [main] c.j.l.s.chapter10.Chapter10Application : Started Chapter10Application in 2.061 seconds (JVM running for 2.486)
2019-03-19 11:04:43.352 INFO 2807 --- [http-nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2019-03-19 11:04:43.353 INFO 2807 --- [http-nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2019-03-19 11:04:43.359 INFO 2807 --- [http-nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 6 ms
2019-03-19 11:04:43.392 INFO 2807 --- [http-nio-8080-exec-1] c.j.l.s.c.c.LoggerDemoController : info(訊息):123
2019-03-19 11:04:43.394 WARN 2807 --- [http-nio-8080-exec-1] c.j.l.s.c.c.LoggerDemoController : warn(警告):123
2019-03-19 11:04:43.395 ERROR 2807 --- [http-nio-8080-exec-1] c.j.l.s.c.c.LoggerDemoController : error(錯誤):123
2019-03-19 11:04:43.395 ERROR 2807 --- [http-nio-8080-exec-1] c.j.l.s.c.c.LoggerDemoController : fatal(致命):123

級別控制

在Spring Boot中只需要在application.properties中進行配置完成日誌記錄的級別控制。

配置格式:logging.level.*=LEVEL

logging.level:日誌級別控制前綴,*為包名或Logger名
LEVEL:選項TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF
舉例:

com.jj.learning.springboot.chapter10.controller
logging.level.com.jj=DEBUG:com.jj包下所有class以DEBUG級別輸出
logging.level.root=WARN:root日誌以WARN級別輸出

  • 修改 application.properties
1
2
3
4
# root日誌以WARN級別輸出
logging.level.root=WARN
# com.jj包下所有class以DEBUG級別輸出
logging.level.com.jj=DEBUG
  • 啟動應用後的日誌
1
2
3
4
5
6
7
8
9
10
11
12
  .   ____          _            __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.3.RELEASE)

2019-03-19 10:54:01.433 INFO 2783 --- [ main] c.j.l.s.chapter10.Chapter10Application : Starting Chapter10Application on localhost with PID 2783 (/Users/morose/Documents/workspace-SpringBoot/chapter-10/target/classes started by morose in /Users/morose/Documents/workspace-SpringBoot/chapter-10)
2019-03-19 10:54:01.435 DEBUG 2783 --- [ main] c.j.l.s.chapter10.Chapter10Application : Running with Spring Boot v2.1.3.RELEASE, Spring v5.1.5.RELEASE
2019-03-19 10:54:01.436 INFO 2783 --- [ main] c.j.l.s.chapter10.Chapter10Application : No active profile set, falling back to default profiles: default
2019-03-19 10:54:03.184 INFO 2783 --- [ main] c.j.l.s.chapter10.Chapter10Application : Started Chapter10Application in 2.15 seconds (JVM running for 2.579)
1
2
3
4
5
2019-03-19 10:56:54.537 DEBUG 2783 --- [nio-8080-exec-1] c.j.l.s.c.c.LoggerDemoController         : debug(調試):123
2019-03-19 10:56:54.539 INFO 2783 --- [nio-8080-exec-1] c.j.l.s.c.c.LoggerDemoController : info(訊息):123
2019-03-19 10:56:54.539 WARN 2783 --- [nio-8080-exec-1] c.j.l.s.c.c.LoggerDemoController : warn(警告):123
2019-03-19 10:56:54.539 ERROR 2783 --- [nio-8080-exec-1] c.j.l.s.c.c.LoggerDemoController : error(錯誤):123
2019-03-19 10:56:54.539 ERROR 2783 --- [nio-8080-exec-1] c.j.l.s.c.c.LoggerDemoController : fatal(致命):123

可以看到日誌輸出的等級為包含Debug以上的日誌


自定義日誌配置

由於日誌服務一般都在ApplicationContext建立前就初始化了,它並不是必須通過Spring的配置文件控制。因此通過系統屬性和傳統的Spring Boot外部配置文件依然可以很好的支持日誌控制和管理。

根據不同的日誌系統,你可以按如下規則組織配置文件名,就能被正確加載:

  • Logback:logback-spring.xml, logback-spring.groovy, logback.xml, logback.groovy
  • Log4j:log4j-spring.properties, log4j-spring.xml, log4j.properties, log4j.xml
  • Log4j2:log4j2-spring.xml, log4j2.xml
  • JDK (Java Util Logging):logging.properties

Spring Boot官方推薦優先使用帶有-spring的文件名作為你的日誌配置(如使用logback-spring.xml,而不是logback.xml)

自定義輸出格式

在Spring Boot中可以通過在application.properties配置如下參數控制輸出格式:

  • logging.pattern.console:定義輸出到控制台的樣式(不支持JDK Logger)
  • logging.pattern.file:定義輸出到文件的樣式(不支持JDK Logger)

註:以上參考了
程序猿DD-翟永超Spring Boot日志管理 文章。