

# Amazon CloudWatch にログを発行するオプションをオンにする
<a name="AuroraPostgreSQL.CloudWatch.Publishing"></a>

Aurora PostgreSQL DB クラスターの PostgreSQL ログを CloudWatch Logs に発行するには、クラスターの **[Log export]** (ログのエクスポート) オプションを選択します。Aurora PostgreSQL DB クラスターを作成する場合、ログのエクスポート設定を選択できます。または、後でクラスターを変更することもできます。既存のクラスターを変更すると、その時点で各インスタンスの PostgreSQL ログが CloudWatch クラスターに発行されます。Aurora PostgreSQL の場合、Amazon CloudWatch に公開されるログは、PostgreSQL ログ (`postgresql.log`) のみです。

AWS マネジメントコンソール、AWS CLI、RDS API を使用して Aurora PostgreSQL DB クラスターのログエクスポート機能をオンにできます。

## コンソール
<a name="AuroraPostgreSQL.CloudWatch.Console"></a>

[Log export] (ログのエクスポート) オプションを選択すると、Aurora PostgreSQL DB クラスターから CloudWatch Logs に PostgreSQL ログの発行が開始されます。

**コンソールからログエクスポート機能をオンにするには**

1. Amazon RDS コンソール ([https://console.aws.amazon.com/rds/](https://console.aws.amazon.com/rds/)) を開きます。

1. ナビゲーションペインで、[**データベース**] を選択します。

1. CloudWatch Logs にログデータを発行する Aurora PostgreSQL DB クラスターを選択します。

1. **Modify** を選択します。

1. **[Log exports]** (ログのエクスポート) セクションで、**[PostgreSQL]** を選択します。

1. [**続行**] を選択し、概要ページで [**クラスターの変更**] を選択します。

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

ログのエクスポートオプションをオンにすると、AWS CLI で Amazon CloudWatch Logs に Aurora PostgreSQL ログの発行が開始されます。これを行うには、以下のオプションを指定して [modify-db-cluster](https://docs.aws.amazon.com/cli/latest/reference/rds/modify-db-cluster.html) AWS CLI コマンドを実行します。
+ `--db-cluster-identifier`— DB クラスター識別子。
+ `--cloudwatch-logs-export-configuration`―DB クラスターで、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 コマンドの 1 つを実行します。
+ `--db-cluster-identifier`— DB クラスター識別子。
+ `--engine` — データベースエンジン。
+ `--enable-cloudwatch-logs-exports` — DB クラスターの CloudWatch Logs へのエクスポートに使用できるログタイプの構成設定。

実行する AWS CLI コマンドに応じて、他のオプションが必要となる場合があります。

**Example**  
次のコマンドでは、ログファイルが CloudWatch Logs に発行されるよう Aurora PostgreSQL DB クラスターを作成します。  
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**  
次のコマンドでは、ログファイルが CloudWatch Logs に発行されるよう既存の Aurora PostgreSQL DB クラスターを変更します。`--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 DB クラスターを変更して、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` － DB クラスター識別子。
+ `CloudwatchLogsExportConfiguration` － DB クラスターの 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`— DB クラスター識別子。
+ `Engine` — データベースエンジン。
+ `EnableCloudwatchLogsExports` — DB クラスターの CloudWatch Logs へのエクスポートに使用できるログタイプの構成設定。

実行する AWS CLI コマンドに応じて、他のパラメータが必要となる場合があります。