

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

# 開啟將日誌發佈到 Amazon CloudWatch 的選項
<a name="AuroraPostgreSQL.CloudWatch.Publishing"></a>

若要將 Aurora PostgreSQL 資料庫叢集的 PostgreSQL 日誌發佈到 CloudWatch Logs，請選擇叢集的 **Log export** (日誌匯出) 選項。您可以在建立 Aurora PostgreSQL 資料庫叢集時選擇日誌匯出設定。或者，您可以稍後修改叢集。當您修改現有叢集時，每個執行個體的 PostgreSQL 日誌會從該時間點開始發佈到 CloudWatch 叢集。對於 Aurora PostgreSQL，PostgreSQL 日誌 (`postgresql.log`) 是唯一發佈到 Amazon CloudWatch 的日誌。

您可以使用 AWS 管理主控台、 AWS CLI或 RDS API 來開啟 Aurora PostgreSQL 資料庫叢集的日誌匯出功能。

## 主控台
<a name="AuroraPostgreSQL.CloudWatch.Console"></a>

您可以選擇日誌匯出選項，開始將 PostgreSQL 日誌從您的 Aurora PostgreSQL 資料庫叢集發佈到 CloudWatch Logs。

**從主控台開啟日誌匯出功能**

1. 前往 [https://console.aws.amazon.com/rds/](https://console.aws.amazon.com/rds/)，開啟 Amazon RDS 主控台。

1. 在導覽窗格中，選擇 **Databases** (資料庫)。

1. 選擇您要發佈日誌資料到 CloudWatch Logs 的 Aurora PostgreSQL 資料庫叢集。

1. 選擇 **Modify (修改)**。

1. 在 **Log exports** (日誌匯出) 區段中，選擇 **Postgresql log** (Postgresql 日誌)。

1. 選擇 **Continue** (繼續)，然後在摘要頁面上選擇 **Modify cluster** (修改叢集)。

## AWS CLI
<a name="AuroraPostgreSQL.CloudWatch.CLI"></a>

您可以開啟日誌匯出選項，開始使用 AWS CLI將 Aurora PostgreSQL 日誌發佈到 Amazon CloudWatch Logs。若要這樣做，請使用下列選項執行 [modify-db-cluster](https://docs.aws.amazon.com/cli/latest/reference/rds/modify-db-cluster.html) AWS CLI 命令：
+ `--db-cluster-identifier`—資料庫叢集識別符
+ `--cloudwatch-logs-export-configuration`—要設定的日誌類型的組態設定，適用於匯出到資料庫叢集的 CloudWatch Logs。

您也可以執行下列其中一個 AWS CLI 命令來發佈 Aurora PostgreSQL 日誌：
+ [create-db-cluster](https://docs.aws.amazon.com/cli/latest/reference/rds/create-db-cluster.html)
+ [restore-db-cluster-from-s3](https://docs.aws.amazon.com/cli/latest/reference/rds/restore-db-cluster-from-s3.html)
+ [restore-db-cluster-from-snapshot](https://docs.aws.amazon.com/cli/latest/reference/rds/restore-db-cluster-from-snapshot.html)
+ [restore-db-cluster-to-point-in-time](https://docs.aws.amazon.com/cli/latest/reference/rds/restore-db-cluster-to-point-in-time.html)

使用以下選項執行其中一個 AWS CLI 命令：
+ `--db-cluster-identifier`—資料庫叢集識別符
+ `--engine` — 資料庫引擎。
+ `--enable-cloudwatch-logs-exports` — 資料庫叢集的哪些日誌類型要匯出至 CloudWatch Logs 的組態設定。

視您執行的 AWS CLI 命令而定，可能需要其他選項。

**Example**  
下列命令建立 Aurora PostgreSQL 資料庫叢集，以將日誌檔案發佈至 CloudWatch Logs。  
對於 Linux、macOS 或 Unix：  

```
1. aws rds create-db-cluster \
2.     --db-cluster-identifier {{my-db-cluster}} \
3.     --engine aurora-postgresql \
4.     --enable-cloudwatch-logs-exports postgresql
```
在 Windows 中：  

```
1. aws rds create-db-cluster ^
2.     --db-cluster-identifier {{my-db-cluster}} ^
3.     --engine aurora-postgresql ^
4.     --enable-cloudwatch-logs-exports postgresql
```

**Example**  
下列命令修改現有的 Aurora PostgreSQL 資料庫叢集，以將日誌檔案發佈至 CloudWatch Logs。`--cloudwatch-logs-export-configuration` 值為 JSON 物件。這個物件的金鑰是 `EnableLogTypes`，而其值為 `postgresql` 和 `instance`。  
對於 Linux、macOS 或 Unix：  

```
1. aws rds modify-db-cluster \
2.     --db-cluster-identifier {{my-db-cluster}} \
3.     --cloudwatch-logs-export-configuration '{"EnableLogTypes":["postgresql","instance"]}'
```
在 Windows 中：  

```
1. aws rds modify-db-cluster ^
2.     --db-cluster-identifier {{my-db-cluster}} ^
3.     --cloudwatch-logs-export-configuration '{\"EnableLogTypes\":[\"postgresql\",\"instance\"]}'
```
使用 Windows 命令提示字元時，務必在 JSON 程式碼中的雙引號 (") 開頭加上反斜線 (\\)，以逸出雙引號。

**Example**  
下列範例會修改現有的 Aurora PostgreSQL 資料庫執行個體叢集，以停用將日誌檔案發佈至 CloudWatch Logs。`--cloudwatch-logs-export-configuration` 值為 JSON 物件。這個物件的金鑰是 `DisableLogTypes`，而其值為 `postgresql` 和 `instance`。  
對於 Linux、macOS 或 Unix：  

```
aws rds modify-db-cluster \
    --db-cluster-identifier {{mydbinstance}} \
    --cloudwatch-logs-export-configuration '{"DisableLogTypes":["postgresql","instance"]}'
```
在 Windows 中：  

```
aws rds modify-db-cluster ^
    --db-cluster-identifier {{mydbinstance}} ^
    --cloudwatch-logs-export-configuration "{\"DisableLogTypes\":[\"postgresql\",\"instance\"]}"
```
使用 Windows 命令提示字元時，您必須在 JSON 程式碼中的雙引號 (") 開頭加上反斜線 (\\)，以逸出雙引號。

## RDS API
<a name="AuroraPostgreSQL.CloudWatch.API"></a>

您可以開啟日誌匯出選項，以開始使用 RDS API 來發佈 Aurora PostgreSQL 日誌。若要執行此動作，您可以執行 [ModifyDBCluster](https://docs.aws.amazon.com/AmazonRDS/latest/APIReference/API_ModifyDBCluster.html) 操作並指定下列選項：
+ `DBClusterIdentifier` – 資料庫叢集識別符。
+ `CloudwatchLogsExportConfiguration` – 要啟用的日誌類型的組態設定，適用於匯出到資料庫叢集的 CloudWatch Logs。

您也可以執行下列其中一個 RDS API 操作，以利用 RDS API 來發佈 Aurora PostgreSQL 日誌：
+ [CreateDBCluster](https://docs.aws.amazon.com/AmazonRDS/latest/APIReference/API_CreateDBCluster.html)
+ [RestoreDBClusterFromS3](https://docs.aws.amazon.com/AmazonRDS/latest/APIReference/API_RestoreDBClusterFromS3.html)
+ [RestoreDBClusterFromSnapshot](https://docs.aws.amazon.com/AmazonRDS/latest/APIReference/API_RestoreDBClusterFromSnapshot.html)
+ [RestoreDBClusterToPointInTime](https://docs.aws.amazon.com/AmazonRDS/latest/APIReference/API_RestoreDBClusterToPointInTime.html)

執行 RDS API 動作並指定下列參數：
+ `DBClusterIdentifier`—資料庫叢集識別符
+ `Engine` — 資料庫引擎。
+ `EnableCloudwatchLogsExports` — 資料庫叢集的哪些日誌類型要匯出至 CloudWatch Logs 的組態設定。

視您執行的 AWS CLI 命令而定，可能需要其他參數。