增強型運作狀態日誌格式 - AWS Elastic Beanstalk

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

增強型運作狀態日誌格式

AWS Elastic Beanstalk 平台使用自訂 Web 伺服器日誌格式,來將 HTTP 請求的相關資訊有效率地轉送至增強式運作狀態報告系統。系統會分析日誌、識別問題,並相應地設定執行個體和環境運作狀態。若您停用環境的 Web 伺服器代理並直接從 Web 容器處理請求,您仍可將伺服器設定為透過 Elastic Beanstalk 運作狀態代理程式使用的位置和格式來輸出日誌,藉此善加運用增強型運作狀態報告。

注意

此頁面上的資訊只和 Linux 類型平台相關。在 Windows Server 平台上,Elastic Beanstalk 會直接從 IIS Web 伺服器接收 HTTP 請求的資訊。如需詳細資訊,請參閱Windows Server 上 IIS 內擷取到的 Web 伺服器指標

Web 伺服器日誌組態

Elastic Beanstalk 平台設定為輸出兩個與 HTTP 請求有關的資訊之日誌。第一個為詳細記錄的格式,並提供有關請求的詳細資訊,包括申請者的使用者代理程式資訊和人類可讀的時間戳記。

/var/log/nginx/access.log

下列範例來自執行於 Ruby Web 伺服器環境上的 nginx 代理,但格式與 Apache 的類似。

172.31.24.3 - - [23/Jul/2015:00:21:20 +0000] "GET / HTTP/1.1" 200 11 "-" "curl/7.22.0 (x86_64-pc-linux-gnu) libcurl/7.22.0 OpenSSL/1.0.1 zlib/1.2.3.4 libidn/1.23 librtmp/2.3" "177.72.242.17" 172.31.24.3 - - [23/Jul/2015:00:21:21 +0000] "GET / HTTP/1.1" 200 11 "-" "curl/7.22.0 (x86_64-pc-linux-gnu) libcurl/7.22.0 OpenSSL/1.0.1 zlib/1.2.3.4 libidn/1.23 librtmp/2.3" "177.72.242.17" 172.31.24.3 - - [23/Jul/2015:00:21:22 +0000] "GET / HTTP/1.1" 200 11 "-" "curl/7.22.0 (x86_64-pc-linux-gnu) libcurl/7.22.0 OpenSSL/1.0.1 zlib/1.2.3.4 libidn/1.23 librtmp/2.3" "177.72.242.17" 172.31.24.3 - - [23/Jul/2015:00:21:22 +0000] "GET / HTTP/1.1" 200 11 "-" "curl/7.22.0 (x86_64-pc-linux-gnu) libcurl/7.22.0 OpenSSL/1.0.1 zlib/1.2.3.4 libidn/1.23 librtmp/2.3" "177.72.242.17" 172.31.24.3 - - [23/Jul/2015:00:21:22 +0000] "GET / HTTP/1.1" 200 11 "-" "curl/7.22.0 (x86_64-pc-linux-gnu) libcurl/7.22.0 OpenSSL/1.0.1 zlib/1.2.3.4 libidn/1.23 librtmp/2.3" "177.72.242.17"

第二個日誌則採用簡潔格式,其中包含僅與增強型運作狀態報告相關的資訊。本日誌會輸出至名為 healthd 的子資料夾,且每小時輪換一次。輪換出的舊日誌會立即刪除。

/var/log/nginx/healthd/application.log.2015-07-23-00

下列範例的日誌採用機器可讀取的格式。

1437609879.311"/"200"0.083"0.083"177.72.242.17 1437609879.874"/"200"0.347"0.347"177.72.242.17 1437609880.006"/bad/path"404"0.001"0.001"177.72.242.17 1437609880.058"/"200"0.530"0.530"177.72.242.17 1437609880.928"/bad/path"404"0.001"0.001"177.72.242.17

增強型運作狀態日誌格式包含下列資訊:

  • 請求的時間,格式為 Unix 時間

  • 請求的路徑

  • 結果的 HTTP 狀態代碼

  • 請求時間

  • 上游時間

  • X-Forwarded-For HTTP 標頭

以 nginx 代理而言,時間為具有三個小數位的浮點秒。Apache 則用整微秒表示。

注意

若您在日誌檔案中看到類似下列的警告 (其中 DATE-TIME 為日期和時間),並且您使用自訂代理 (例如在多容器 Docker 環境中的情況),您必須使用 .ebextension 來設定您的環境,讓 healthd 可讀取您的日誌檔案:

W, [DATE-TIME #1922] WARN -- : log file "/var/log/nginx/healthd/application.log.DATE-TIME" does not exist

您可以從多容器 Docker 範例中的 .ebextension 開始。

/etc/nginx/conf.d/webapp_healthd.conf

下列範例為 nginx 的日誌組態,並反白顯示 healthd 日誌格式。

upstream my_app { server unix:///var/run/puma/my_app.sock; } log_format healthd '$msec"$uri"' '$status"$request_time"$upstream_response_time"' '$http_x_forwarded_for'; server { listen 80; server_name _ localhost; # need to listen to localhost for worker tier if ($time_iso8601 ~ "^(\d{4})-(\d{2})-(\d{2})T(\d{2})") { set $year $1; set $month $2; set $day $3; set $hour $4; } access_log /var/log/nginx/access.log main; access_log /var/log/nginx/healthd/application.log.$year-$month-$day-$hour healthd; location / { proxy_pass http://my_app; # match the name of upstream directive which is defined above proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } location /assets { alias /var/app/current/public/assets; gzip_static on; gzip on; expires max; add_header Cache-Control public; } location /public { alias /var/app/current/public; gzip_static on; gzip on; expires max; add_header Cache-Control public; } }
/etc/httpd/conf.d/healthd.conf

以下範例顯示 Apache 的日誌組態。

LogFormat "%{%s}t\"%U\"%s\"%D\"%D\"%{X-Forwarded-For}i" healthd CustomLog "|/usr/sbin/rotatelogs /var/log/httpd/healthd/application.log.%Y-%m-%d-%H 3600" healthd

產生增強型運作狀態報告的日誌

欲提供日誌給運作狀態代理程式,您必須執行下列操作:

  • 如上述以正確格式輸出日誌

  • 將日誌輸出至 /var/log/nginx/healthd/

  • 日誌命名格式如下:application.log.$year-$month-$day-$hour

  • 每小時輪換日誌一次

  • 請勿截斷日誌