本系列程式碼地址:https://github.com/HashZhang/spring-cloud-scaffold/tree/master/spring-cloud-iiford

  1. server:
  2. undertow:
  3. # access log相關配置
  4. accesslog:
  5. # 存放目錄,預設為 logs
  6. dir: ./log
  7. # 是否開啟
  8. enabled: true
  9. # 格式,各種佔位符後面會詳細說明
  10. pattern: '{
  11. "transportProtocol":"%{TRANSPORT_PROTOCOL}",
  12. "scheme":"%{SCHEME}",
  13. "protocol":"%{PROTOCOL}",
  14. "method":"%{METHOD}",
  15. "reqHeaderUserAgent":"%{i,User-Agent}",
  16. "cookieUserId": "%{c,userId}",
  17. "queryTest": "%{q,test}",
  18. "queryString": "%q",
  19. "relativePath": "%R, %{REQUEST_PATH}, %{RESOLVED_PATH}",
  20. "requestLine": "%r",
  21. "uri": "%U",
  22. "thread": "%I",
  23. "hostPort": "%{HOST_AND_PORT}",
  24. "localIp": "%A",
  25. "localPort": "%p",
  26. "localServerName": "%v",
  27. "remoteIp": "%a",
  28. "remoteHost": "%h",
  29. "bytesSent": "%b",
  30. "time":"%{time,yyyy-MM-dd HH:mm:ss.S}",
  31. "status":"%s",
  32. "reason":"%{RESPONSE_REASON_PHRASE}",
  33. "respHeaderUserSession":"%{o,userSession}",
  34. "respCookieUserId":"%{resp-cookie,userId}",
  35. "timeUsed":"%Dms, %Ts, %{RESPONSE_TIME}ms, %{RESPONSE_TIME_MICROS} us, %{RESPONSE_TIME_NANOS} ns",
  36. }'
  37. # 檔案字首,預設為 access_log
  38. prefix: access.
  39. # 檔案字尾,預設為 log
  40. suffix: log
  41. # 是否另起日誌檔案寫 access log,預設為 true
  42. # 目前只能按照日期進行 rotate,一天一個日誌檔案
  43. rotate: true

Undertow 的 accesslog 處理核心類抽象是 io.undertow.server.handlers.accesslog.AccesslogReceiver。由於目前 Undertow 的 AccesslogReceiver 只有一種實現在使用,也就是 io.undertow.server.handlers.accesslog.DefaultAccessLogReceiver

檢視 DefaultAccessLogReceiver 的 rotate 時機:

DefaultAccessLogReceiver

  1. /**
  2. * 計算 rotate 時間點
  3. */
  4. private void calculateChangeOverPoint() {
  5. Calendar calendar = Calendar.getInstance();
  6. calendar.set(Calendar.SECOND, 0);
  7. calendar.set(Calendar.MINUTE, 0);
  8. calendar.set(Calendar.HOUR_OF_DAY, 0);
  9. //當前時間日期 + 1,即下一天
  10. calendar.add(Calendar.DATE, 1);
  11. SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
  12. currentDateString = df.format(new Date());
  13. // if there is an existing default log file, use the date last modified instead of the current date
  14. if (Files.exists(defaultLogFile)) {
  15. try {
  16. currentDateString = df.format(new Date(Files.getLastModifiedTime(defaultLogFile).toMillis()));
  17. } catch(IOException e){
  18. // ignore. use the current date if exception happens.
  19. }
  20. }
  21. //rotate 時機是下一天的 0 點
  22. changeOverPoint = calendar.getTimeInMillis();
  23. }

其實 Undertow 中的 accesslog 佔位符,就是之前我們提到的 Undertow Listener 解析請求後抽象的 HTTP server exchange 的屬性。

官網文件的表格並不是最全的,並且注意點並沒有說明,例如某些佔位符必須開啟某些 Undertow 特性才能使用等等。這裡我們列出下。

首先先提出一個注意點,引數佔位符,例如 %{i,你要看的header值} 檢視 header 的某個 key 的值。逗號後面注意不要有空格,因為這個空格會算入 key 裡面導致拿不到你想要的 key

請求相關屬性

描述 縮寫佔位符 全名佔位符 引數佔位符 原始碼
請求傳輸協議,等價於請求協議 %{TRANSPORT_PROTOCOL} TransportProtocolAttribute
請求模式,例如 http、https 等 %{SCHEME} RequestSchemeAttribute
請求協議,例如 HTTP/1.1 %H %{PROTOCOL} RequestProtocolAttribute
請求方法,例如 GET、POST 等 %m %{METHOD} RequestMethodAttribute
請求 Header 的某一個值 %{i,你要看的header值} RequestHeaderAttribute
Cookie 的某一個值 %{c,你要看的cookie值} 或者 %{req-cookie,你要看的cookie值} 分別對應 CookieAttributeRequestCookieAttribute
路徑引數 PathVariable 由於並沒有被 Undertow 的 Listener 或者 Handler 解析處理,所以攔截不到,無法確認是否是一個 PathVariable 還是就是 url 路徑。所以,PathVariable 的佔位符是不會起作用的 %{p, 你想檢視的路徑引數 key } PathParameterAttribute
請求引數,即 url 的 ? 之後鍵值對,這裡可以選擇檢視某個 key 的值。 %{q, 你想檢視的請求引數 key} QueryParameterAttribute
請求引數字串,即 url 的 ? 之後的所有字元} %q(不包含 ?) %{QUERY_STRING}(不包含 ?);%{BARE_QUERY_STRING}(包含 ?) QueryStringAttribute
請求相對路徑(在 Spring Boot 環境下,大多數情況 RequestPath 和 RelativePath 還有 ResolvedPath 是等價的),即除去 host,port,請求引數字串的路徑 %R %{RELATIVE_PATH} 或者 %{REQUEST_PATH} 或者 %{RESOLVED_PATH} 分別對應 RelativePathAttributeRequestPathAttributeResolvedPathAttribute
請求整體字串,包括請求方法,請求相對路徑,請求引數字串,請求協議,例如 Get /test?a=b HTTP/1.1 %r %{REQUEST_LINE} RequestLineAttribute
請求 URI,包括請求相對路徑,請求引數字串 %U %{REQUEST_URL} RequestURLAttribute
處理請求的執行緒 %I %{THREAD_NAME} ThreadNameAttribute

注意:

  1. 路徑引數 PathVariable 由於並沒有被 Undertow 的 Listener 或者 Handler 解析處理,所以攔截不到,無法確認是否是一個 PathVariable 還是就是 url 路徑。所以,PathVariable 的佔位符是不會起作用的

請求地址相關

描述 縮寫佔位符 全名佔位符 引數佔位符 原始碼
host 和 port,一般就是 HTTP 請求 Header 中的 Host 值,如果 Host 為空則獲取本地地址和埠,如果沒獲取到埠則根據協議用預設埠(http:80,,https:443) %{HOST_AND_PORT} HostAndPortAttribute
請求本地地址 IP %A %{LOCAL_IP} LocalIPAttribute
請求本地埠 Port %p %{LOCAL_PORT} LocalPortAttribute
請求本地主機名,一般就是 HTTP 請求 Header 中的 Host 值,如果 Host 為空則獲取本地地址 %v %{LOCAL_SERVER_NAME} LocalServerNameAttribute
請求遠端主機名,通過連接獲取遠端的主機地址 %h %{REMOTE_HOST} RemoteHostAttribute
請求遠端 IP,通過連接獲取遠端的 IP %a %{REMOTE_IP} RemoteIPAttribute

注意:

  1. 請求的遠端地址我們一般不從請求連接獲取,而是通過 Http Header 裡面的 X-forwarded-for 或者 X-real-ip 等獲取,因為現在請求都是通過各種 VPN,負載均衡器發上來的。

響應相關屬性

描述 縮寫佔位符 全名佔位符 引數佔位符 原始碼
傳送的位元組數大小,除了 Http Header 以外 %b (如果為空就是 -) 或者 %B (如果為空就是 0) %{BYTES_SENT} (如果為空就是 0) BytesSentAttribute
accesslog 時間,這個不是收到請求的時間,而是響應的時間 %t %{DATE_TIME} %{time, 你自定義的 java 中 SimpleDateFormat 的格式} DateTimeAttribute
HTTP 響應狀態碼 %s %{RESPONSE_CODE} ResponseCodeAttribute
HTTP 響應原因 %{RESPONSE_REASON_PHRASE} ResponseReasonPhraseAttribute
響應 Header 的某一個值 %{o,你要看的header值} ResponseHeaderAttribute
響應 Cookie 的某一個值 %{resp-cookie,你要看的cookie值} ResponseCookieAttribute
響應時間,預設 undertow 沒有開啟請求時間內統計,需要開啟才能統計響應時間 %D(毫秒,例如 56 代表 56ms) %T(秒,例如 5.067 代表 5.067 秒) %{RESPONSE_TIME}(等價於 %D) %{RESPONSE_TIME_MICROS} (微秒) %{RESPONSE_TIME_NANOS}(納秒) ResponseTimeAttribute

注意:預設 undertow 沒有開啟請求時間內統計,需要開啟才能統計響應時間,如何開啟呢?通過註冊一個 WebServerFactoryCustomizer 到 Spring ApplicationContext 中即可。請看下面的程式碼(專案地址:https://github.com/HashZhang/spring-cloud-scaffold/blob/master/spring-cloud-iiford/):

spring.factories(省略無關程式碼)

  1. # AutoConfiguration
  2. org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  3. com.github.hashjang.spring.cloud.iiford.service.common.auto.UndertowAutoConfiguration

UndertowAutoConfiguration

  1. //設定proxyBeanMethods=false,因為沒有 @Bean 的方法互相呼叫需要每次返回同一個 Bean,沒必要代理,關閉增加啟動速度
  2. @Configuration(proxyBeanMethods = false)
  3. @Import(WebServerConfiguration.class)
  4. public class UndertowAutoConfiguration {
  5. }

WebServerConfiguration

  1. //設定proxyBeanMethods=false,因為沒有 @Bean 的方法互相呼叫需要每次返回同一個 Bean,沒必要代理,關閉增加啟動速度
  2. @Configuration(proxyBeanMethods = false)
  3. public class WebServerConfiguration {
  4. @Bean
  5. public WebServerFactoryCustomizer<ConfigurableUndertowWebServerFactory> undertowWebServerAccessLogTimingEnabler(ServerProperties serverProperties) {
  6. return new DefaultWebServerFactoryCustomizer(serverProperties);
  7. }
  8. }

DefaultWebServerFactoryCustomizer

  1. public class DefaultWebServerFactoryCustomizer implements WebServerFactoryCustomizer<ConfigurableUndertowWebServerFactory> {
  2. private final ServerProperties serverProperties;
  3. public DefaultWebServerFactoryCustomizer(ServerProperties serverProperties) {
  4. this.serverProperties = serverProperties;
  5. }
  6. @Override
  7. public void customize(ConfigurableUndertowWebServerFactory factory) {
  8. String pattern = serverProperties.getUndertow().getAccesslog().getPattern();
  9. // 如果 accesslog 配置中列印了響應時間,則開啟記錄請求開始時間配置
  10. if (logRequestProcessingTiming(pattern)) {
  11. factory.addBuilderCustomizers(builder -> builder.setServerOption(UndertowOptions.RECORD_REQUEST_START_TIME, true));
  12. }
  13. }
  14. private boolean logRequestProcessingTiming(String pattern) {
  15. if (StringUtils.isBlank(pattern)) {
  16. return false;
  17. }
  18. //判斷 accesslog 是否配置了檢視響應時間
  19. return pattern.contains(ResponseTimeAttribute.RESPONSE_TIME_MICROS)
  20. || pattern.contains(ResponseTimeAttribute.RESPONSE_TIME_MILLIS)
  21. || pattern.contains(ResponseTimeAttribute.RESPONSE_TIME_NANOS)
  22. || pattern.contains(ResponseTimeAttribute.RESPONSE_TIME_MILLIS_SHORT)
  23. || pattern.contains(ResponseTimeAttribute.RESPONSE_TIME_SECONDS_SHORT);
  24. }
  25. }

其他

還有安全相關的屬性(SSL 相關,登入認證 Authentication 相關),微服務內部呼叫一般用不到,我們這裡就不贅述了。

其它內建的屬性,在 Spring Boot 環境下一般用不到,我們這裡就不討論了。

舉例

我們最開始配置的 accesslog 的例子請求返回如下( JSON 格式化之後的結果):

  1. {
  2. "transportProtocol": "http/1.1",
  3. "scheme": "http",
  4. "protocol": "HTTP/1.1",
  5. "method": "GET",
  6. "reqHeaderUserAgent": "PostmanRuntime/7.26.10",
  7. "cookieUserId": "testRequestCookieUserId",
  8. "queryTest": "1",
  9. "queryString": "?test=1&query=2",
  10. "relativePath": "/test, /test, -",
  11. "requestLine": "GET /test?test=1&query=2 HTTP/1.1",
  12. "uri": "/test",
  13. "thread": "XNIO-2 task-1",
  14. "hostPort": "127.0.0.1:8102",
  15. "localIp": "127.0.0.1",
  16. "localPort": "8102",
  17. "localServerName": "127.0.0.1",
  18. "remoteIp": "127.0.0.1",
  19. "remoteHost": "127.0.0.1",
  20. "bytesSent": "26",
  21. "time": "2021-04-08 00:07:50.410",
  22. "status": "200",
  23. "reason": "OK",
  24. "respHeaderUserSession": "testResponseHeaderUserSession",
  25. "respCookieUserId": "testResponseCookieUserId",
  26. "timeUsed": "3683ms, 3.683s, 3683ms, 3683149 us, 3683149200 ns",
  27. }

我們這一節詳細介紹瞭如何配置 Undertow 的 accesslog,將 accesslog 各種佔位符都羅列了出來,使用者可以根據這些資訊配置出自己想要的 accesslog 資訊以及格式。下一節,我們將詳細介紹我們框架中針對 Undertow 的定製程式碼

微信搜尋“我的程式設計喵”關注公眾號,每日一刷,輕鬆提升技術,斬獲各種offer