

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

# 設定標準記錄 (v2)
<a name="standard-logging"></a>

您可以在建立或更新分佈時啟用存取日誌 （標準日誌）。標準記錄 (v2) 包含下列功能：
+ 將存取日誌傳送至 Amazon CloudWatch Logs、Amazon Data Firehose 與 Amazon Simple Storage Service (Amazon S3)。
+ 選取您想要的日誌欄位。您也可以選取[即時存取日誌欄位的子集](#standard-logging-real-time-log-selection)。
+ 選取其他[輸出日誌檔案格式](#supported-log-file-format)。

如果您使用的是 Amazon S3，您有下列選用功能：
+ 將日誌傳送至選擇加入 AWS 區域。
+ 使用分割組織您的日誌。
+ 啟用與 Hive 相容的檔案名稱。

如需詳細資訊，請參閱[傳送日誌至 Amazon S3](#send-logs-s3)。

如要開始使用標準記錄，請完成以下步驟：

1. 為 AWS 服務 將接收日誌的指定 設定所需的許可。

1. 從 CloudFront 主控台或 CloudWatch API 設定標準記錄。

1. 檢視您的存取日誌。

**注意**  
如果您啟用標準記錄 (v2)，這不會影響或變更標準記錄 (舊版)。除了使用標準記錄 (v2) 之外，您還可以繼續使用分佈的標準記錄 (舊版)。如需詳細資訊，請參閱[設定標準記錄 (舊式)](standard-logging-legacy-s3.md)。
如果您已啟用標準記錄 (舊版)，而且想要啟用 Amazon S3 的標準記錄 (v2)，建議您指定*不同的* Amazon S3 儲存貯體，或使用相同儲存貯體中的*個別路徑* (例如，使用日誌字首或分割)。這可協助您追蹤哪些日誌檔案與哪些分佈相關聯，並防止日誌檔案互相覆寫。

## 許可
<a name="permissions-standard-logging"></a>

CloudFront 使用 CloudWatch 提供的日誌來傳遞存取日誌。若要這樣做，您需要指定 的許可， AWS 服務 才能啟用記錄交付。

若要查看每個記錄目的地的必要許可，請在 *Amazon CloudWatch Logs 使用者指南*中選擇下列主題。
+ [CloudWatch Logs](https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/AWS-logs-and-resource-policy.html#AWS-logs-infrastructure-V2-CloudWatchLogs)
+ [Firehose](https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/AWS-logs-and-resource-policy.html#AWS-logs-infrastructure-V2-Firehose)
+ [Amazon S3](https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/AWS-logs-and-resource-policy.html#AWS-logs-infrastructure-V2-S3)

在設定記錄目的地的許可後，您便可以為分佈啟用標準記錄。

**注意**  
CloudFront 支援將存取日誌傳送至不同的 AWS 帳戶 （跨帳戶）。若要啟用跨帳戶交付，帳戶 (您的帳戶和接收帳戶) 必須具有必要的許可。如需詳細資訊，請參閱《Amazon CloudWatch Logs 使用者指南》**中的 [啟用跨帳戶交付的標準記錄](#enable-standard-logging-cross-accounts) 一節或[跨帳戶交付範例](https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/AWS-logs-and-resource-policy.html#vended-logs-crossaccount-example)。

## 啟用標準記錄
<a name="set-up-standard-logging"></a>

若要啟用標準記錄，您可以使用 CloudFront 主控台或 CloudWatch API。

**Contents**
+ [

### 啟用標準記錄 (CloudFront 主控台)
](#access-logging-console)
+ [

### 啟用標準記錄 (CloudWatch API)
](#enable-access-logging-api)

### 啟用標準記錄 (CloudFront 主控台)
<a name="access-logging-console"></a>

**啟用 CloudFront 分佈的標準記錄 (主控台)**

1. 使用 CloudFront 主控台以[更新現有的分佈](HowToUpdateDistribution.md#HowToUpdateDistributionProcedure)。

1. 選擇 **Logging** (日誌記錄) 索引標籤。

1. 選擇**新增**，然後選擇要接收日誌的服務：
   + CloudWatch Logs
   + Firehose
   + Amazon S3

1. 針對**目的地**，請選取您服務的資源。如果您尚未建立資源，您可以選擇**建立**或查看下列文件。
   + 針對 CloudWatch Logs，輸入**[日誌群組名稱](https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/Working-with-log-groups-and-streams.html)**。
   + 對於 Firehose，輸入 **[Firehose 交付串流](https://docs.aws.amazon.com/firehose/latest/dev/basic-create.html)**。
   + 針對 Amazon S3，輸入**[儲存貯體名稱](https://docs.aws.amazon.com/AmazonS3/latest/userguide/create-bucket-overview.html)**。
**提示**  
若要指定字首，請在儲存貯體名稱後面輸入字首，例如 `amzn-s3-demo-bucket.s3.amazonaws.com/MyLogPrefix`。如果您未指定字首，CloudFront 會自動為您新增字首。如需詳細資訊，請參閱[傳送日誌至 Amazon S3](#send-logs-s3)。

1. 對於**其他設定 – *選用***，您可以指定下列選項：

   1. 針對**欄位選擇**，選取您要交付至目的地的日誌欄位名稱。您可以選取[存取日誌欄位](standard-logs-reference.md#BasicDistributionFileFormat)和[即時存取日誌欄位](#standard-logging-real-time-log-selection)的子集。

   1. (僅限 Amazon S3) 對於**分割**，指定分割日誌檔案資料的路徑。

   1. (僅限 Amazon S3) 對於 **Hive 相容檔案格式**，您可以選取核取方塊以使用 Hive 相容 S3 路徑。這有助於簡化將新資料載入 Hive 相容工具的過程。

   1. 針對**輸出格式**，請指定您偏好的格式。
**注意**  
如果您選擇 **Parquet**，此選項會產生將存取日誌轉換為 Apache Parquet 的 CloudWatch 費用。如需詳細資訊，請參閱 [CloudWatch 定價中的付費日誌](https://aws.amazon.com/cloudwatch/pricing/)一節。

   1. 對於**欄位分隔符號**，指定如何分隔日誌欄位。

1. 完成更新或建立分佈的步驟。

1. 若要新增另一個目的地，請重複步驟 3–6。

1. 在**日誌**頁面中，確認分佈旁的標準日誌狀態為**已啟用**。

1. (選用) 若要啟用 Cookie 記錄，請選擇**管理**、**設定**並開啟 **Cookie 記錄**，然後選擇**儲存變更**。
**提示**  
Cookie 記錄是全域設定，適用於分佈的*所有*標準日誌。您無法覆寫個別交付目的地的此設定。

如需標準記錄交付和日誌欄位的詳細資訊，請參閱 [標準記錄參考](standard-logs-reference.md)。

### 啟用標準記錄 (CloudWatch API)
<a name="enable-access-logging-api"></a>

您也可以使用 CloudWatch API 為您的分佈啟用標準記錄。

**備註**  
呼叫 CloudWatch API 以啟用標準記錄時，即使您想要啟用跨區域交付到另一個目的地，您也必須指定美國東部 (維吉尼亞北部) 區域 (`us-east-1`)。例如，如果您想要將存取日誌傳送至歐洲 (愛爾蘭) 區域的 S3 儲存貯體 (`eu-west-1`)，請使用 `us-east-1` 區域中的 CloudWatch API。
在標準日誌中包含 Cookie 還有其他選項。在 CloudFront API 中，這是 `IncludeCookies` 參數。如果您透過使用 CloudWatch API 設定存取記錄，並指定要包含 Cookie，則必須使用 CloudFront 主控台或 CloudFront API 來更新您的分佈以包含 Cookie。否則，CloudFront 無法傳送 Cookie 到您的日誌目的地。如需詳細資訊，請參閱[Cookie 記錄](DownloadDistValuesGeneral.md#DownloadDistValuesCookieLogging)。

**啟用分佈的標準記錄 (CloudWatch API)**

1. 建立分佈之後，請取得 Amazon Resource Name (ARN)。

   您可以從 CloudFront 主控台的**分佈**頁面找到 ARN，也可以使用 [GetDistribution](https://docs.aws.amazon.com/cloudfront/latest/APIReference/API_GetDistribution.html) API 操作。分佈 ARN 遵循以下格式：`arn:aws:cloudfront::123456789012:distribution/d111111abcdef8`

1. 接著，使用 CloudWatch [PutDeliverySource](https://docs.aws.amazon.com/AmazonCloudWatchLogs/latest/APIReference/API_PutDeliverySource.html) API 操作來建立分佈的交付來源。

   1. 輸入交付來源的名稱。

   1. 傳遞分佈的 `resourceArn`。

   1. 對於 `logType`，指定 `ACCESS_LOGS` 為收集的日誌類型。

   1.   
**Example AWS CLI put-delivery-source 命令**  

      以下是為分佈設定交付來源的範例。

      ```
      aws logs put-delivery-source --name S3-delivery --resource-arn arn:aws:cloudfront::123456789012:distribution/d111111abcdef8 --log-type ACCESS_LOGS
      ```

      **輸出**

      ```
      {
       "deliverySource": {
       "name": "S3-delivery",
       "arn": "arn:aws:logs:us-east-1:123456789012:delivery-source:S3-delivery",
       "resourceArns": [
       "arn:aws:cloudfront::123456789012:distribution/d111111abcdef8"
       ],
       "service": "cloudfront",
       "logType": "ACCESS_LOGS"
       }
      }
      ```

1. 使用 [PutDeliveryDestination](https://docs.aws.amazon.com/AmazonCloudWatchLogs/latest/APIReference/API_PutDeliveryDestination.html) API 操作來設定存放日誌的位置。

   1. 針對 `destinationResourceArn`，指定目的地的 ARN。這可以是 CloudWatch Logs 日誌群組、Firehose 交付串流，或 Amazon S3 儲存貯體。

   1. 針對 `outputFormat`，指定日誌的輸出格式。

   1.   
**Example AWS CLI put-delivery-destination 命令**  

      以下是將交付目的地設定為 Amazon S3 儲存貯體的範例。

      ```
      aws logs put-delivery-destination --name S3-destination --delivery-destination-configuration destinationResourceArn=arn:aws:s3:::amzn-s3-demo-bucket
      ```

      **輸出**

      ```
      {
          "name": "S3-destination",
          "arn": "arn:aws:logs:us-east-1:123456789012:delivery-destination:S3-destination",
          "deliveryDestinationType": "S3",
          "deliveryDestinationConfiguration": {
              "destinationResourceArn": "arn:aws:s3:::amzn-s3-demo-bucket"
          }
      }
      ```
**注意**  
如果您要跨帳戶傳遞日誌，則必須使用 [PutDeliveryDestinationPolicy](https://docs.aws.amazon.com/AmazonCloudWatchLogs/latest/APIReference/API_PutDeliveryDestinationPolicy.html) API 操作，以便將 AWS Identity and Access Management (IAM) 政策指派給目的地。IAM 政策允許從一個帳戶交付到另一個帳戶。

1. 使用 [CreateDelivery](https://docs.aws.amazon.com/AmazonCloudWatchLogs/latest/APIReference/API_CreateDelivery.html) API 操作，將交付來源連結至您在先前步驟中建立的目的地。此 API 操作會將交付來源與最終目的地建立關聯。

   1. 針對 `deliverySourceName`，指定來源名稱。

   1. 針對 `deliveryDestinationArn`，指定交付目的地的 ARN。

   1. 對於 `fieldDelimiter`，指定要分隔每個日誌欄位的字串。

   1. 針對 `recordFields`，指定您想要的日誌欄位。

   1. 如果您使用的是 S3，請指定是否使用 `enableHiveCompatiblePath` 和 `suffixPath`。  
**Example AWS CLI create-delivery 命令範例**  

   以下為建立交付的範例。

   ```
   aws logs create-delivery --delivery-source-name cf-delivery --delivery-destination-arn arn:aws:logs:us-east-1:123456789012:delivery-destination:S3-destination
   ```

   **輸出**

   ```
   {
       "id": "abcNegnBoTR123",
       "arn": "arn:aws:logs:us-east-1:123456789012:delivery:abcNegnBoTR123",
       "deliverySourceName": "cf-delivery",
       "deliveryDestinationArn": "arn:aws:logs:us-east-1:123456789012:delivery-destination:S3-destination",
       "deliveryDestinationType": "S3",
       "recordFields": [
           "date",
           "time",
           "x-edge-location",
           "sc-bytes",
           "c-ip",
           "cs-method",
           "cs(Host)",
           "cs-uri-stem",
           "sc-status",
           "cs(Referer)",
           "cs(User-Agent)",
           "cs-uri-query",
           "cs(Cookie)",
           "x-edge-result-type",
           "x-edge-request-id",
           "x-host-header",
           "cs-protocol",
           "cs-bytes",
           "time-taken",
           "x-forwarded-for",
           "ssl-protocol",
           "ssl-cipher",
           "x-edge-response-result-type",
           "cs-protocol-version",
           "fle-status",
           "fle-encrypted-fields",
           "c-port",
           "time-to-first-byte",
           "x-edge-detailed-result-type",
           "sc-content-type",
           "sc-content-len",
           "sc-range-start",
           "sc-range-end",
           "c-country",
           "cache-behavior-path-pattern"
       ],
        "fieldDelimiter": ""
   }
   ```

1. 在 CloudFront 主控台的**日誌**頁面上，確認分佈旁的標準日誌狀態**已啟用**。

   如需標準記錄交付和日誌欄位的詳細資訊，請參閱 [標準記錄參考](standard-logs-reference.md)。

**注意**  
若要使用 啟用 CloudFront 的標準記錄 (v2) AWS CloudFormation，您可以使用下列 CloudWatch Logs 屬性：  
[Delivery](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-logs-delivery.html)
[DeliveryDestination](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-logs-deliverydestination.html)
[DeliverySource](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-logs-deliverysource.html)
`ResourceArn` 是 CloudFront 分佈，且 `LogType` 必須是 `ACCESS_LOGS` 作為支援的日誌類型。

## 啟用跨帳戶交付的標準記錄
<a name="enable-standard-logging-cross-accounts"></a>

如果您為 啟用標準記錄， AWS 帳戶 而且想要將存取日誌交付給另一個帳戶，請確定您正確設定來源帳戶和目的地帳戶。具有 CloudFront 分佈的*來源帳戶*會將其存取日誌傳送至*目的地帳戶*。

在此範例程序中，來源帳戶 (*111111111111*) 會將其存取日誌傳送至目的地帳戶 (*222222222222*) 中的 Amazon S3 儲存貯體。若要將存取日誌傳送至目的地帳戶中的 Amazon S3 儲存貯體，請使用 AWS CLI。

### 設定目的地帳戶
<a name="steps-destination-account"></a>

對於目的地帳戶，請完成下列程序。

**設定目的地帳戶**

1. 若要建立日誌交付目的地，您可以輸入下列 AWS CLI 命令。此範例使用 `MyLogPrefix` 字串為您的存取日誌建立字首。

   ```
   aws logs put-delivery-destination --name cloudfront-delivery-destination --delivery-destination-configuration "destinationResourceArn=arn:aws:s3:::amzn-s3-demo-bucket-cloudfront-logs/MyLogPrefix"
   ```

   **輸出**

   ```
   {
       "deliveryDestination": {
           "name": "cloudfront-delivery-destination",
           "arn": "arn:aws:logs:us-east-1:222222222222:delivery-destination:cloudfront-delivery-destination",
           "deliveryDestinationType": "S3",
           "deliveryDestinationConfiguration": {"destinationResourceArn": "arn:aws:s3:::amzn-s3-demo-bucket-cloudfront-logs/MyLogPrefix"}
       }
   }
   ```
**注意**  
如果您指定的 S3 儲存貯體*沒有*字首，則 CloudFront 會自動將 `AWSLogs/<account-ID>/CloudFront` 附加為字首，顯示在 S3 交付目的地的 `suffixPath` 中。如需詳細資訊，請參閱 [S3DeliveryConfiguration](https://docs.aws.amazon.com/AmazonCloudWatchLogs/latest/APIReference/API_S3DeliveryConfiguration.html)。

1. 新增日誌交付目的地的資源政策，以允許來源帳戶建立日誌交付。

   在下列政策中，以來源帳戶 ID 取代 *111111111111*，並從步驟 1 的輸出指定交付目的地 ARN。

------
#### [ JSON ]

****  

   ```
   {
       "Version":"2012-10-17",		 	 	 
       "Statement": [
           {
               "Sid": "AllowCreateDelivery",
               "Effect": "Allow",
               "Principal": {"AWS": "111111111111"},
               "Action": ["logs:CreateDelivery"],
               "Resource": "arn:aws:logs:us-east-1:222222222222:delivery-destination:cloudfront-delivery-destination"
           }
       ]
   }
   ```

------

1. 儲存檔案，例如 `deliverypolicy.json`。

1. 若要將先前的政策連接至交付目的地，請輸入下列 AWS CLI 命令。

   ```
   aws logs put-delivery-destination-policy --delivery-destination-name cloudfront-delivery-destination --delivery-destination-policy file://deliverypolicy.json
   ```

1. 將下列陳述式新增至目的地 Amazon S3 儲存貯體政策，取代資源 ARN 和來源帳戶 ID。此政策允許 `delivery.logs.amazonaws.com` 服務主體執行 `s3:PutObject` 動作。

   ```
   {
       "Sid": "AWSLogsDeliveryWrite",
       "Effect": "Allow",
       "Principal": {"Service": "delivery.logs.amazonaws.com"},
       "Action": "s3:PutObject",
       "Resource": "arn:aws:s3:::amzn-s3-demo-bucket-cloudfront-logs/*",
       "Condition": {
           "StringEquals": {
               "s3:x-amz-acl": "bucket-owner-full-control",
               "aws:SourceAccount": "111111111111"
           },
           "ArnLike": {"aws:SourceArn": "arn:aws:logs:us-east-1:111111111111:delivery-source:*"}
       }
   }
   ```

1. 如果您使用 AWS KMS 做為儲存貯體，請將下列陳述式新增至 KMS 金鑰政策，以將許可授予`delivery.logs.amazonaws.com`服務主體。

   ```
   {
       "Sid": "Allow Logs Delivery to use the key",
       "Effect": "Allow",
       "Principal": {"Service": "delivery.logs.amazonaws.com"},
       "Action": [
           "kms:Encrypt",
           "kms:Decrypt",
           "kms:ReEncrypt*",
           "kms:GenerateDataKey*",
           "kms:DescribeKey"
       ],
       "Resource": "*",
       "Condition": {
           "StringEquals": {"aws:SourceAccount": "111111111111"},
           "ArnLike": {"aws:SourceArn": "arn:aws:logs:us-east-1:111111111111:delivery-source:*"}
       }
   }
   ```

### 設定來源帳戶
<a name="steps-source-account"></a>

設定目的地帳戶之後，請依照此程序建立交付來源，並啟用來源帳戶中分佈的記錄。

**設定來源帳戶**

1. 建立 CloudFront 標準記錄的交付來源，以便您可以將日誌檔案傳送至 CloudWatch Logs。

   您可以輸入下列 AWS CLI 命令，取代名稱和分佈 ARN。

   ```
   aws logs put-delivery-source --name s3-cf-delivery --resource-arn arn:aws:cloudfront::111111111111:distribution/E1TR1RHV123ABC --log-type ACCESS_LOGS
   ```

   **輸出**

   ```
   {
       "deliverySource": {
           "name": "s3-cf-delivery",
           "arn": "arn:aws:logs:us-east-1:111111111111:delivery-source:s3-cf-delivery",
           "resourceArns": ["arn:aws:cloudfront::111111111111:distribution/E1TR1RHV123ABC"],
           "service": "cloudfront",
           "logType": "ACCESS_LOGS"
       }
   }
   ```

1. 建立交付以對應來源帳戶的日誌交付來源和目的地帳戶的日誌交付目的地。

   在下列 AWS CLI 命令中，從[步驟 1：設定目的地帳戶中的輸出指定交付目的地](#steps-destination-account) ARN。

   ```
   aws logs create-delivery --delivery-source-name s3-cf-delivery --delivery-destination-arn arn:aws:logs:us-east-1:222222222222:delivery-destination:cloudfront-delivery-destination
   ```

   **輸出**

   ```
   {
       "delivery": {
           "id": "OPmOpLahVzhx1234",
           "arn": "arn:aws:logs:us-east-1:111111111111:delivery:OPmOpLahVzhx1234",
           "deliverySourceName": "s3-cf-delivery",
           "deliveryDestinationArn": "arn:aws:logs:us-east-1:222222222222:delivery-destination:cloudfront-delivery-destination",
           "deliveryDestinationType": "S3",
           "recordFields": [
               "date",
               "time",
               "x-edge-location",
               "sc-bytes",
               "c-ip",
               "cs-method",
               "cs(Host)",
               "cs-uri-stem",
               "sc-status",
               "cs(Referer)",
               "cs(User-Agent)",
               "cs-uri-query",
               "cs(Cookie)",
               "x-edge-result-type",
               "x-edge-request-id",
               "x-host-header",
               "cs-protocol",
               "cs-bytes",
               "time-taken",
               "x-forwarded-for",
               "ssl-protocol",
               "ssl-cipher",
               "x-edge-response-result-type",
               "cs-protocol-version",
               "fle-status",
               "fle-encrypted-fields",
               "c-port",
               "time-to-first-byte",
               "x-edge-detailed-result-type",
               "sc-content-type",
               "sc-content-len",
               "sc-range-start",
               "sc-range-end",
               "c-country",
               "cache-behavior-path-pattern"
           ],
           "fieldDelimiter": "\t"
       }
   }
   ```

1. 驗證您的跨帳戶交付是否成功。

   1. 從*來源*帳戶登入 CloudFront 主控台，然後選擇您的分佈。在**記錄**索引標籤的**類型**下，您會看到為 S3 跨帳戶日誌交付建立的項目。

   1. 從*目的地*帳戶登入 Amazon S3 主控台，然後選擇您的 Amazon S3 儲存貯體。您會在儲存貯體名稱中看到字首 `MyLogPrefix`，以及傳送到該資料夾的任何存取日誌。

## 輸出檔案格式
<a name="supported-log-file-format"></a>

根據您選擇的交付目的地，您可以為日誌檔指定下列其中一種格式：
+ JSON
+ Plain
+ w3c
+ Raw
+ Parquet (僅限 Amazon S3)

**注意**  
您只能在第一次建立交付目的地時設定輸出格式。這無法在稍後更新。若要變更輸出格式，請刪除交付項目，然後建立另一種格式。

如需詳細資訊，請參閱《Amazon CloudWatch Logs API 參考指南》**中的 [PutDeliveryDestination](https://docs.aws.amazon.com/AmazonCloudWatchLogs/latest/APIReference/API_PutDeliveryDestination.html)。

## 編輯標準記錄設定
<a name="standard-logs-v2-edit-settings"></a>

您可以使用 [CloudFront 主控台](https://console.aws.amazon.com/cloudfront/v4/home)或 CloudWatch API 啟用或停用記錄和更新其他日誌設定。記錄設定的變更在 12 小時內生效。

如需詳細資訊，請參閱下列主題：
+ 若要使用 CloudFront 主控台更新分佈，請參閱 [更新分佈](HowToUpdateDistribution.md)。
+ 若要使用 CloudFront API 更新分佈，請參閱《Amazon CloudFront API 參考》**中的 [UpdateDistribution](https://docs.aws.amazon.com/cloudfront/latest/APIReference/API_UpdateDistribution.html)。
+ 如需 CloudWatch Logs API 操作的詳細資訊，請參閱《[Amazon CloudWatch Logs API 參考](https://docs.aws.amazon.com/AmazonCloudWatchLogs/latest/APIReference/Welcome.html)》。

## 存取日誌欄位
<a name="standard-logging-real-time-log-selection"></a>

您可以選取標準記錄 (舊版) 支援的相同日誌欄位。如需詳細資訊，請參閱[日誌檔案欄位](standard-logs-reference.md#BasicDistributionFileFormat)。

此外，您可以選取下列[即時存取日誌欄位](real-time-logs.md#understand-real-time-log-config)。

1. `timestamp(ms)` – 以毫秒為單位的時間戳記。

1. `origin-fbl` – CloudFront 與您的原始伺服器之間第一個位元組延遲的秒數。

1. `origin-lbl` – CloudFront 與您的原始伺服器之間最後一個位元組延遲的秒數。

1. `asn` – 檢視器的自治系統編號 (ASN)。

1. `c-country` – 國家/地區代碼代表檢視器的地理位置，由檢視器的 IP 位址決定。如需國家/地區代碼的清單，請參閱 [ISO 3166-1 alpha-2](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2)。

1. `cache-behavior-path-pattern` – 識別符合檢視器請求之快取行為的路徑模式。

## 將日誌傳送至 CloudWatch Logs
<a name="send-logs-cloudwatch-logs"></a>

若要將日誌傳送至 CloudWatch Logs，請建立或使用現有的 CloudWatch Logs 日誌群組。如需有關設定 CloudWatch Logs 日誌群組的詳細資訊，請參閱[使用日誌群組和日誌串流](https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/Working-with-log-groups-and-streams.html)。

建立日誌群組之後，您必須擁有允許標準記錄的必要許可。如需有關必要許可的詳細資訊，請參閱《Amazon CloudWatch Logs 使用者指南》**中的[傳送至 CloudWatch Logs 的日誌](https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/AWS-logs-and-resource-policy.html#AWS-logs-infrastructure-V2-CloudWatchLogs)。

**備註**  
當您指定 CloudWatch Logs 日誌群組的名稱時，請僅使用 regex 模式 `[\w-]`。如需詳細資訊，請參閱《Amazon CloudWatch Logs API 參考》**中的 [PutDeliveryDestination](https://docs.aws.amazon.com/AmazonCloudWatchLogs/latest/APIReference/API_PutDeliveryDestination.html#API_PutDeliveryDestination_RequestSyntax) API 操作。
確認您的日誌群組資源政策未超過大小限制。請參閱 CloudWatch Logs 主題中的[日誌群組資源政策大小限制考量](https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/AWS-logs-and-resource-policy.html#AWS-logs-infrastructure-V2-CloudWatchLogs)一節。

### 傳送至 CloudWatch Logs 的存取日誌範例
<a name="example-access-logs-cwl"></a>

```
{ 
"date": "2024-11-14", 
"time": "21:34:06", 
"x-edge-location": "SOF50-P2", 
"asn": "16509", 
"timestamp(ms)": "1731620046814", 
"origin-fbl": "0.251", 
"origin-lbl": "0.251", 
"x-host-header": "d111111abcdef8.cloudfront.net", 
"cs(Cookie)": "examplecookie=value" 
}
```

## 傳送日誌至 Firehose
<a name="send-logs-kinesis"></a>

若要將日誌傳送至 Firehose，請建立或使用現有的 Firehose 交付串流。然後，指定 Firehose 交付串流做為日誌交付目的地。您必須在美國東部 (維吉尼亞北部) us-east-1 區域中指定 Firehose 交付串流。

如需建立交付串流的相關資訊，請參閱[建立 Amazon Data Firehose 交付串流](https://docs.aws.amazon.com/firehose/latest/dev/basic-create.html)。

建立交付串流後，您必須擁有允許標準記錄的必要許可。如需詳細資訊，請參閱《Amazon CloudWatch Logs 使用者指南》**中的[傳送至 Firehose 的日誌](https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/AWS-logs-and-resource-policy.html#AWS-logs-infrastructure-V2-Firehose)。

**注意**  
當您指定 Firehose 串流的名稱時，請僅使用 regex 模式 `[\w-]`。如需詳細資訊，請參閱《Amazon CloudWatch Logs API 參考》**中的 [PutDeliveryDestination](https://docs.aws.amazon.com/AmazonCloudWatchLogs/latest/APIReference/API_PutDeliveryDestination.html#API_PutDeliveryDestination_RequestSyntax) API 操作。

### 傳送至 Firehose 的存取日誌範例
<a name="example-access-logs-firehose"></a>

```
{"date":"2024-11-15","time":"19:45:51","x-edge-location":"SOF50-P2","asn":"16509","timestamp(ms)":"1731699951183","origin-fbl":"0.254","origin-lbl":"0.254","x-host-header":"d111111abcdef8.cloudfront.net","cs(Cookie)":"examplecookie=value"}
{"date":"2024-11-15","time":"19:45:52","x-edge-location":"SOF50-P2","asn":"16509","timestamp(ms)":"1731699952950","origin-fbl":"0.125","origin-lbl":"0.125","x-host-header":"d111111abcdef8.cloudfront.net","cs(Cookie)":"examplecookie=value"}
```

## 傳送日誌至 Amazon S3
<a name="send-logs-s3"></a>

若要將存取日誌傳送至 Amazon S3，請建立或使用現有的 S3 儲存貯體。當您在 CloudFront 中啟用記錄時，請指定儲存貯體名稱。如需建立儲存貯體的詳細資訊，請參閱《Amazon Simple Storage Service 使用者指南》**中的[建立儲存貯體](https://docs.aws.amazon.com/AmazonS3/latest/userguide/create-bucket-overview.html)。

建立儲存貯體之後，您必須擁有允許標準記錄的必要許可。如需詳細資訊，請參閱「Amazon CloudWatch Logs 使用者指南」**中的[傳送至 Amazon S3 的日誌](https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/AWS-logs-and-resource-policy.html#AWS-logs-infrastructure-V2-S3)。
+ 啟用記錄後， AWS 會自動為您新增必要的儲存貯體政策。
+ 您也可以在[選擇加入 AWS 區域](https://docs.aws.amazon.com/accounts/latest/reference/manage-acct-regions.html) 中使用 S3 儲存貯體。

**注意**  
如果您已啟用標準記錄 (舊版)，而且想要啟用 Amazon S3 的標準記錄 (v2)，建議您指定*不同的* Amazon S3 儲存貯體，或使用相同儲存貯體中的*個別路徑* (例如，使用日誌字首或分割)。這可協助您追蹤哪些日誌檔案與哪些分佈相關聯，並防止日誌檔案互相覆寫。

**Topics**
+ [

### 指定 S3 儲存貯體
](#prefix-s3-buckets)
+ [

### 分割
](#partitioning)
+ [

### 與 Hive 相容的檔案名稱格式
](#hive-compatible-file-name-format)
+ [

### 存取日誌的範例路徑
](#bucket-path-examples)
+ [

### 傳送至 Amazon S3 的存取日誌範例
](#example-access-logs-s3)

### 指定 S3 儲存貯體
<a name="prefix-s3-buckets"></a>

當您指定 S3 儲存貯體做為交付目的地時，請注意下列事項。

S3 儲存貯體名稱只能使用 regex 模式 `[\w-]`。如需詳細資訊，請參閱《Amazon CloudWatch Logs API 參考》**中的 [PutDeliveryDestination](https://docs.aws.amazon.com/AmazonCloudWatchLogs/latest/APIReference/API_PutDeliveryDestination.html#API_PutDeliveryDestination_RequestSyntax) API 操作。

如果您為 S3 儲存貯體指定字首，您的日誌會顯示在該路徑下。如果您不指定字首，CloudFront 會自動為您附加 `AWSLogs/{account-id}/CloudFront` 字首。

如需詳細資訊，請參閱[存取日誌的範例路徑](#bucket-path-examples)。

### 分割
<a name="partitioning"></a>

當 CloudFront 將存取日誌傳送到 S3 儲存貯體時，您可以使用分割來組織存取日誌。這有助於您根據需要的路徑來組織和找出存取日誌。

您可以使用下列變數來建立資料夾路徑。
+ `{DistributionId}` 或 `{distributionid}`
+ `{yyyy}`
+ `{MM}`
+ `{dd}`
+ `{HH}`
+ `{accountid}`

您可以使用任意數量的變數，並指定路徑中的資料夾名稱。接著，CloudFront 會使用此路徑，在 S3 儲存貯體中為您建立資料夾結構。

**範例**
+ `my_distribution_log_data/{DistributionId}/logs`
+ `/cloudfront/{DistributionId}/my_distribution_log_data/{yyyy}/{MM}/{dd}/{HH}/logs `

**注意**  
 您可以使用任一變數做為尾碼路徑中的分佈 ID。不過，如果您要將存取日誌傳送到 AWS Glue，則必須使用 `{distributionid}`變數，因為 AWS Glue 預期分割區名稱為小寫。在 CloudFront 中更新現有的日誌組態，以 `{DistributionId}` 取代 `{distributionid}`。

### 與 Hive 相容的檔案名稱格式
<a name="hive-compatible-file-name-format"></a>

您可以使用此選項，讓包含交付存取日誌的 S3 物件使用允許與 Apache Hive 整合的字首結構。如需詳細資訊，請參閱 [CreateDelivery](https://docs.aws.amazon.com/AmazonCloudWatchLogs/latest/APIReference/API_CreateDelivery.html) API 操作。

**Example 範例**  

```
/cloudfront/DistributionId={DistributionId}/my_distribution_log_data/year={yyyy}/month={MM}/day={dd}/hour={HH}/logs
```

如需分割和 Hive 相容選項的詳細資訊，請參閱《Amazon CloudWatch Logs API 參考》**中的 [S3DeliveryConfiguration](https://docs.aws.amazon.com/AmazonCloudWatchLogs/latest/APIReference/API_S3DeliveryConfiguration.html) 元素。

### 存取日誌的範例路徑
<a name="bucket-path-examples"></a>

當您指定 S3 儲存貯體做為目的地時，您可以使用下列選項來建立存取日誌的路徑：
+ 包含或不含字首的 Amazon S3 儲存貯體
+ 透過使用 CloudFront 提供的變數或輸入您自己變數的分割
+ 啟用 Hive 相容選項

下表顯示您的存取日誌在儲存貯體中，依您選擇的選項所顯示的方式。

#### 具有字首的 Amazon S3 儲存貯體
<a name="bucket-with-prefix"></a>


| Amazon S3 儲存貯體名稱。 | 您在尾碼路徑中指定的分割區 | 已更新尾碼路徑 | 已啟用 Hive 相容？ | 存取日誌會傳送至 | 
| --- | --- | --- | --- | --- | 
| amzn-s3-demo-bucket/MyLogPrefix | 無 | 無 | 否 | amzn-s3-demo-bucket/MyLogPrefix/ | 
| amzn-s3-demo-bucket/MyLogPrefix | myFolderA/ | myFolderA/ | 否 | amzn-s3-demo-bucket/MyLogPrefix/myFolderA/ | 
| amzn-s3-demo-bucket/MyLogPrefix | myFolderA/\$1yyyy\$1 | myFolderA/\$1yyyy\$1 | 是 | amzn-s3-demo-bucket/MyLogPrefix/myFolderA/year=2025 | 

#### 沒有字首的 Amazon S3 儲存貯體
<a name="bucket-without-prefix"></a>


| Amazon S3 儲存貯體名稱。 | 您在尾碼路徑中指定的分割區 | 已更新尾碼路徑 | 已啟用 Hive 相容？ | 存取日誌會傳送至 | 
| --- | --- | --- | --- | --- | 
| amzn-s3-demo-bucket | 無 | AWSLogs/\$1account-id\$1/CloudFront/ | 否 | amzn-s3-demo-bucket/AWSLogs/<your-account-ID>/CloudFront/ | 
| amzn-s3-demo-bucket | myFolderA/ | AWSLogs/\$1account-id\$1/CloudFront/myFolderA/ | 否 | amzn-s3-demo-bucket/AWSLogs/<your-account-ID>/CloudFront/myFolderA/ | 
| amzn-s3-demo-bucket | myFolderA/ | AWSLogs/\$1account-id\$1/CloudFront/myFolderA/ | 是 | amzn-s3-demo-bucket/AWSLogs/aws-account-id=<your-account-ID>/CloudFront/myFolderA/ | 
| amzn-s3-demo-bucket | myFolderA/\$1yyyy\$1 | AWSLogs/\$1account-id\$1/CloudFront/myFolderA/\$1yyyy\$1 | 是 | amzn-s3-demo-bucket/AWSLogs/aws-account-id=<your-account-ID>/CloudFront/myFolderA/year=2025 | 

#### AWS 帳戶 ID 做為分割區
<a name="bucket-account-id-partition"></a>


| Amazon S3 儲存貯體名稱。 | 您在尾碼路徑中指定的分割區 | 已更新尾碼路徑 | 已啟用 Hive 相容？ | 存取日誌會傳送至 | 
| --- | --- | --- | --- | --- | 
| amzn-s3-demo-bucket | 無 | AWSLogs/\$1account-id\$1/CloudFront/ | 是 | amzn-s3-demo-bucket/AWSLogs/aws-account-id=<your-account-ID>/CloudFront/ | 
| amzn-s3-demo-bucket | myFolderA/\$1accountid\$1 | AWSLogs/\$1account-id\$1/CloudFront/myFolderA/\$1accountid\$1 | 是 | amzn-s3-demo-bucket/AWSLogs/aws-account-id=<your-account-ID>/CloudFront/myFolderA/accountid=<your-account-ID> | 

**備註**  
`{account-id}` 變數會保留給 CloudFront。如果您指定*沒有*字首的 Amazon S3 儲存貯體，CloudFront 會自動將此變數新增至尾碼路徑。如果您的日誌與 Hive 相容，此變數會顯示為 `aws-account-id`。
您可以使用 `{accountid}` 變數，讓 CloudFront 將您的帳戶 ID 新增至尾碼路徑。如果您的日誌與 Hive 相容，此變數會顯示為 `accountid`。
如需尾碼路徑的詳細資訊，請參閱 [S3DeliveryConfiguration](https://docs.aws.amazon.com/AmazonCloudWatchLogs/latest/APIReference/API_S3DeliveryConfiguration.html)。

### 傳送至 Amazon S3 的存取日誌範例
<a name="example-access-logs-s3"></a>

```
#Fields: date time x-edge-location asn timestamp(ms) x-host-header cs(Cookie)
2024-11-14    22:30:25    SOF50-P2    16509    1731623425421    
d111111abcdef8.cloudfront.net    examplecookie=value2
```

## 停用標準記錄
<a name="delete-standard-log-destination"></a>

如果您不再需要分佈的標準記錄，您可以停用該記錄。

**停用標準記錄**

1. 登入 CloudFront 主控台。

1. 選擇**分佈**，然後選擇分佈 ID。

1. 選擇**記錄**，然後在**存取日誌目的地**下，選取目的地。

1. 選擇**管理**，然後選擇**刪除**。

1. 如果您有多個標準記錄，請重複上一個步驟。

**注意**  
當您從 CloudFront 主控台刪除標準記錄時，此動作只會刪除交付和交付目的地。它不會從您的 刪除交付來源 AWS 帳戶。若要刪除交付來源，請在 `aws logs delete-delivery-source --name DeliverySourceName` 命令中指定交付來源名稱。如需詳細資訊，請參閱《Amazon CloudWatch Logs API 參考》**中的 [DeleteDeliverySource](https://docs.aws.amazon.com/AmazonCloudWatchLogs/latest/APIReference/API_DeleteDeliverySource.html)。

## 疑難排解
<a name="troubleshooting-access-logs-v2"></a>

使用下列資訊來修正使用 CloudFront 標準記錄 (v2) 時的常見問題。

### 交付來源已存在
<a name="access-logging-resource-already-used"></a>

當您為分佈啟用標準記錄時，您可以建立交付來源。然後，您可以使用該交付來源來建立您想要的目的地類型交付：CloudWatch Logs、Firehose、Amazon S3。目前，每個分佈只能有一個交付來源。如果您嘗試為相同的分佈建立另一個交付來源，則會出現下列錯誤訊息。

```
This ResourceId has already been used in another Delivery Source in this account
```

若要建立另一個交付來源，請先刪除現有的交付來源。如需詳細資訊，請參閱《Amazon CloudWatch Logs API 參考》**中的 [DeleteDeliverySource](https://docs.aws.amazon.com/AmazonCloudWatchLogs/latest/APIReference/API_DeleteDeliverySource.html)。

### 我變更了尾碼路徑，而 Amazon S3 儲存貯體無法接收我的日誌
<a name="access-logging-s3-permission"></a>

如果您啟用標準記錄 (v2) 並指定無字首的儲存貯體 ARN，CloudFront 會將下列預設值附加到尾碼路徑：`AWSLogs/{account-id}/CloudFront`。如果您使用 CloudFront 主控台或 [UpdateDeliveryConfiguration](https://docs.aws.amazon.com/AmazonCloudWatchLogs/latest/APIReference/API_UpdateDeliveryConfiguration.html) API 操作來指定不同的尾碼路徑，則必須更新 Amazon S3 儲存貯體政策以使用相同的路徑。

**Example 範例：更新尾碼路徑**  

1. 您的預設尾碼路徑為 `AWSLogs/{account-id}/CloudFront`，而您將其取代為 `myFolderA`。

1. 由於您的新尾碼路徑與 Amazon S3 儲存貯體政策中指定的路徑不同，因此不會交付您的存取日誌。

1. 您可以執行下列任一步驟：
   + 將 Amazon S3 儲存貯體許可從 `amzn-s3-demo-bucket/AWSLogs/<your-account-ID>/CloudFront/*` 更新為 `amzn-s3-demo-bucket/myFolderA/*`。
   + 更新您的記錄組態以再次使用預設尾碼：`AWSLogs/{account-id}/CloudFront`
如需詳細資訊，請參閱[許可](#permissions-standard-logging)。

## 刪除日誌檔案
<a name="standard-logs-v2-delete"></a>

CloudFront 不會自動從您的目的地刪除日誌檔案。如需有關刪除日誌檔案的詳細資訊，請參閱下列主題：

**Amazon S3**
+ [Amazon Simple Storage Service 主控台使用者指南](https://docs.aws.amazon.com/AmazonS3/latest/userguide/DeletingObjects.html)中的*刪除物件*

**CloudWatch Logs**
+ *Amazon CloudWatch Logs 使用者指南*中的[使用日誌群組和日誌串流](https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/Working-with-log-groups-and-streams.html)
+ *Amazon CloudWatch Logs API 參考*中的 [DeleteLogGroup](https://docs.aws.amazon.com/AmazonCloudWatchLogs/latest/APIReference/API_DeleteLogGroup.html) 

**Firehose**
+ *Amazon Data Firehose API 參考*中的 [DeleteDeliveryStream](https://docs.aws.amazon.com/firehose/latest/APIReference/API_DeleteDeliveryStream.html) 

## 定價
<a name="pricing-standard-logs"></a>

CloudFront 不收取啟用標準日誌的費用。不過，您可能需要支付交付、擷取、儲存或存取的費用，視您選取的日誌交付目的地而定。如需詳細資訊，請參閱 [Amazon CloudWatch Logs 定價](https://aws.amazon.com/cloudwatch/pricing/)。在**付費方案**下，選擇**日誌**索引標籤，然後在**已付費日誌**下，查看每個交付目的地的資訊。

如需每個 定價的詳細資訊 AWS 服務，請參閱下列主題：
+ [Amazon CloudWatch Logs 定價](https://aws.amazon.com/cloudwatch/pricing/)
+ [Amazon Data Firehose 定價](https://aws.amazon.com/kinesis/data-firehose/pricing/)
+ [Amazon S3 定價](https://aws.amazon.com/s3/pricing/) 
**注意**  
雖然儲存和存取日誌檔案會產生 Amazon S3 費用，但將日誌傳送到 Amazon S3 不會產生額外費用。如果您啟用 **Parquet** 選項將存取日誌轉換為 Apache Parquet，此選項會產生 CloudWatch 費用。如需詳細資訊，請參閱 [CloudWatch 定價中的付費日誌](https://aws.amazon.com/cloudwatch/pricing/)一節。