是預設的日誌框架, EP 8 建立了設定檔,配置了 Console 及 File 兩種日誌輸出格式。
Table of Contents
Toggle前言
Logging 是一個日誌框架,提供日常的開發,測試和生產環境中,記錄了應用,服務運行過程中的訊息,以及出現異常時的例外,這些資訊常常作為查詢,定位,解決問題的關鍵。
Spring Boot Logging
檔案目錄
./
+- build.gradle
| +- src
| +- main
| +- resources
| | +- application.properties
| | +- logback.xml
+- logs
+- spring-boot-168.log
Gradle
build.gradle
增加 Web 。
修改完後,點右鍵,Gradle -> Refresh Gradle Project 。
buildscript {
group 'org.ruoxue.spring-boot-168'
version = '0.0.1-SNAPSHOT'
ext {
springBootVersion = '2.1.7.RELEASE'
}
}
dependencies {
implementation "org.springframework.boot:spring-boot-starter-web:${springBootVersion}"
}
設定
logback.xml
新增檔案,增加以下配置。
<pre class="xml">
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true">
<property name="LOG_PATTERN"
value="%d{yyyy-MM-dd'T'HH:mm:ss.SSSZ} [%thread] %p %c{1}#%M:%L - %m%n%ex" />
<property name="LOG_PATH"
value="logs" />
<property name="LOG_FILE"
value="spring-boot-168" />
<appender name="CONSOLE"
class="ch.qos.logback.core.ConsoleAppender">
<encoder charset="UTF-8">
<pattern>${LOG_PATTERN}</pattern>
</encoder>
</appender>
<appender name="FILE"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_PATH}/${LOG_FILE}.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<fileNamePattern>${LOG_PATH}/${LOG_FILE}-%d{yyyy-MM-dd}-%i.log.gz</fileNamePattern>
<minIndex>1</minIndex>
<maxIndex>10</maxIndex>
</rollingPolicy>
<encoder charset="UTF-8">
<pattern>${LOG_PATTERN}</pattern>
</encoder>
<triggeringPolicy
class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<maxFileSize>100MB</maxFileSize>
</triggeringPolicy>
</appender>
<root level="INFO">
<appender-ref ref="CONSOLE" level="INFO"/>
<appender-ref ref="FILE" level="INFO"/>
</root>
<logger name="org.springframework.jdbc" level="INFO">
<appender-ref ref="CONSOLE" />
</logger>
</configuration>
</pre>
application.properties
增加 Logging 設定。
logging.config=classpath:logback.xml
spring.output.ansi.enabled=DETECT
測試
Application.main
在主程式點右鍵執行 Run As -> Java Application,查看 console,Server 已成功啟動,監聽 10000 port,並在此目錄下輸出成檔案, logs/spring-boot-168.log。

. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.7.RELEASE)
2022-10-15T14:31:12.932+0800 [main] INFO o.r.s.Application#logStarting:50 - Starting Application on cheng-pc with PID 12588 (D:\dev\gitlab\ruoxue\spring-boot-168\bin\main started by cheng in D:\dev\gitlab\ruoxue\spring-boot-168)
2022-10-15T14:31:12.935+0800 [main] INFO o.r.s.Application#logStartupProfileInfo:647 - No active profile set, falling back to default profiles: default
[GC (Metadata GC Threshold) 130563K->15155K(1005056K), 0.0090316 secs]
[Full GC (Metadata GC Threshold) 15155K->14380K(1005056K), 0.0172712 secs]
2022-10-15T14:31:13.727+0800 [main] INFO o.s.b.w.e.j.JettyServletWebServerFactory#getWebServer:143 - Server initialized with port: 10000
2022-10-15T14:31:13.729+0800 [main] INFO o.e.j.s.Server#doStart:370 - jetty-9.4.19.v20190610; built: 2019-06-10T16:30:51.723Z; git: afcf563148970e98786327af5e07c261fda175d3; jvm 1.8.0_241-b07
2022-10-15T14:31:13.749+0800 [main] INFO o.e.j.s.h.C.application#log:2351 - Initializing Spring embedded WebApplicationContext
2022-10-15T14:31:13.750+0800 [main] INFO o.s.w.c.ContextLoader#prepareWebApplicationContext:284 - Root WebApplicationContext: initialization completed in 785 ms
2022-10-15T14:31:13.815+0800 [main] INFO o.e.j.s.session#doStart:365 - DefaultSessionIdManager workerName=node0
2022-10-15T14:31:13.816+0800 [main] INFO o.e.j.s.session#doStart:370 - No SessionScavenger set, using defaults
2022-10-15T14:31:13.816+0800 [main] INFO o.e.j.s.session#startScavenging:149 - node0 Scavenging every 660000ms
2022-10-15T14:31:13.835+0800 [main] INFO o.e.j.s.h.C.application#log:2351 - Warning: No org.apache.tomcat.JarScanner set in ServletContext. Falling back to default JarScanner implementation.
2022-10-15T14:31:13.941+0800 [main] INFO o.a.j.s.TldScanner#info:119 - At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
2022-10-15T14:31:13.946+0800 [main] INFO o.e.j.s.h.ContextHandler#doStart:857 - Started o.s.b.w.e.j.JettyEmbeddedWebAppContext@22df874e{application,/,[file:///C:/Users/cheng/AppData/Local/Temp/jetty-docbase.258792775600776518.10000/],AVAILABLE}
2022-10-15T14:31:13.947+0800 [main] INFO o.e.j.s.Server#doStart:410 - Started @1655ms
2022-10-15T14:31:14.066+0800 [main] INFO o.s.s.c.ThreadPoolTaskExecutor#initialize:171 - Initializing ExecutorService 'applicationTaskExecutor'
[GC (Metadata GC Threshold) 200514K->32261K(1005056K), 0.0094879 secs]
[Full GC (Metadata GC Threshold) 32261K->21686K(1005056K), 0.0255164 secs]
2022-10-15T14:31:14.248+0800 [main] INFO o.e.j.s.h.C.application#log:2351 - Initializing Spring DispatcherServlet 'dispatcherServlet'
2022-10-15T14:31:14.249+0800 [main] INFO o.s.w.s.DispatcherServlet#initServletBean:524 - Initializing Servlet 'dispatcherServlet'
2022-10-15T14:31:14.253+0800 [main] INFO o.s.w.s.DispatcherServlet#initServletBean:546 - Completed initialization in 4 ms
2022-10-15T14:31:14.334+0800 [main] INFO o.e.j.s.AbstractConnector#doStart:292 - Started ServerConnector@a486d78{HTTP/1.1,[http/1.1]}{0.0.0.0:10000}
2022-10-15T14:31:14.335+0800 [main] INFO o.s.b.w.e.j.JettyWebServer#start:156 - Jetty started on port(s) 10000 (http/1.1) with context path '/'
2022-10-15T14:31:14.337+0800 [main] INFO o.r.s.Application#logStarted:59 - Started Application in 1.662 seconds (JVM running for 2.045)
心得分享
使用 Log4j2 或是 Logback 都可以協助在開發或維運時,系統發生異常狀況,快速定位問題,進而尋找合適的解決方案。