

# 开启将日志发布到 Amazon CloudWatch 的选项
<a name="AuroraPostgreSQL.CloudWatch.Publishing"></a>

要将 Aurora PostgreSQL 数据库集群的 PostgreSQL 日志发布到 CloudWatch Logs，请选择集群的 **Log export**（日志导出）选项。在创建 Aurora PostgreSQL 数据库集群时，您可以选择 Log export（日志导出）设置。或者，您可以稍后修改集群。当您修改现有集群时，其每个实例的 PostgreSQL 日志将从那时起发布到 CloudWatch 集群。对于 Aurora PostgreSQL，PostgreSQL 日志（`postgresql.log`）是唯一发布到 Amazon CloudWatch 的日志。

您可以使用 AWS 管理控制台、AWS CLI 或 RDS API 为您的 Aurora PostgreSQL 数据库集群开启 Log export（日志导出）功能。

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

您可以选择 Log export（日志导出）选项，以开始将 PostgreSQL 日志从 Aurora PostgreSQL 数据库集群发布到 CloudWatch Logs。

**从控制台开启 Log export（日志导出）功能**

1. 通过以下网址打开 Amazon RDS 控制台：[https://console.aws.amazon.com/rds/](https://console.aws.amazon.com/rds/)。

1. 在导航窗格中，选择 **Databases**（数据库）。

1. 选择要将其日志数据发布到 CloudWatch Logs 的 Aurora PostgreSQL 数据库集群。

1. 选择**修改**。

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。为此，请使用以下选项运行 AWS CLI 命令 [modify-db-cluster](https://docs.aws.amazon.com/cli/latest/reference/rds/modify-db-cluster.html)：
+ `--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 代码中转义双引号 (")，方法是使用反斜杠 (\$1) 作为其前缀。

**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 代码中转义双引号 (")，方法是使用反斜杠 (\$1) 作为其前缀。

## 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 命令，可能需要其他参数。