網頁

2019/9/1

Spring Boot 預設的日誌級別 default log level

Spring Boot預設的日誌等級為INFO。也就是預設會輸出ERRORWARNINFO的log訊息。

例如當Spring Boot專案啟動時,可以看到日誌在console印出訊息,訊息的級別即為INFO


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

2019-09-01 21:14:55.145  INFO 11780 --- [           main] c.a.d.SpringbootWebApplication           : Starting SpringbootWebApplication on matt-PC with PID 11780 (D:\MyProject\workspace\springboot-web\bin\main started by matt in D:\MyProject\workspace\springboot-web)
2019-09-01 21:14:55.161  INFO 11780 --- [           main] c.a.d.SpringbootWebApplication           : No active profile set, falling back to default profiles: default
2019-09-01 21:14:57.401  INFO 11780 --- [           main] o.s.b.w.e.t.TomcatWebServer              : Tomcat initialized with port(s): 8080 (http)
2019-09-01 21:14:57.456  INFO 11780 --- [           main] o.a.c.c.StandardService                  : Starting service [Tomcat]
2019-09-01 21:14:57.459  INFO 11780 --- [           main] o.a.c.c.StandardEngine                   : Starting Servlet engine: [Apache Tomcat/9.0.22]
2019-09-01 21:14:57.605  INFO 11780 --- [           main] o.a.c.c.C.[.[.[/demo]                    : Initializing Spring embedded WebApplicationContext
2019-09-01 21:14:57.605  INFO 11780 --- [           main] o.s.w.c.ContextLoader                    : Root WebApplicationContext: initialization completed in 2299 ms
2019-09-01 21:14:58.069  INFO 11780 --- [           main] o.s.s.c.ThreadPoolTaskExecutor           : Initializing ExecutorService 'applicationTaskExecutor'
2019-09-01 21:14:58.385  INFO 11780 --- [           main] o.s.b.w.e.t.TomcatWebServer              : Tomcat started on port(s): 8080 (http) with context path '/demo'
2019-09-01 21:14:58.391  INFO 11780 --- [           main] c.a.d.SpringbootWebApplication   

不過一旦Sprinb Boot改用了log4j2框架並添加了log4j2的設定檔如log4j2.properties,即使在沒設定任何東西的情況下,Logger預設等級會改為log4j2的預設等級ERROR。啟動後僅會印出以下。

因為當Spring Boot找到log4j2.properties代表日誌輸出的相關設定改由log4j2處理,所以我們必須在log4j2.properties自己定義要如何輸出日誌。


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

log4j2.properties增加以下設定來在console輸出日誌。

log4j2.properties

appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
 
rootLogger.level = INFO
rootLogger.appenderRefs = stdout
rootLogger.appenderRef.stdout.ref = STDOUT

啟動專案會印出下面結果。


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

2019-09-02 00:00:31 INFO  SpringbootWebApplication:50 - Starting SpringbootWebApplication on matt-PC with PID 7304 (D:\MyProject\workspace\springboot-web\bin\main started by matt in D:\MyProject\workspace\springboot-web)
2019-09-02 00:00:31 INFO  SpringbootWebApplication:647 - No active profile set, falling back to default profiles: default
2019-09-02 00:00:33 INFO  TomcatWebServer:90 - Tomcat initialized with port(s): 8080 (http)
2019-09-02 00:00:33 INFO  Http11NioProtocol:173 - Initializing ProtocolHandler ["http-nio-8080"]
2019-09-02 00:00:33 INFO  StandardService:173 - Starting service [Tomcat]
2019-09-02 00:00:33 INFO  StandardEngine:173 - Starting Servlet engine: [Apache Tomcat/9.0.22]
2019-09-02 00:00:33 INFO  [/demo]:173 - Initializing Spring embedded WebApplicationContext
2019-09-02 00:00:33 INFO  ContextLoader:284 - Root WebApplicationContext: initialization completed in 1977 ms
2019-09-02 00:00:34 INFO  ThreadPoolTaskExecutor:171 - Initializing ExecutorService 'applicationTaskExecutor'
2019-09-02 00:00:34 INFO  Http11NioProtocol:173 - Starting ProtocolHandler ["http-nio-8080"]
2019-09-02 00:00:34 INFO  TomcatWebServer:202 - Tomcat started on port(s): 8080 (http) with context path '/demo'
2019-09-02 00:00:34 INFO  SpringbootWebApplication:59 - Started SpringbootWebApplication in 3.365 seconds (JVM running for 4.303)


參考:

沒有留言:

張貼留言