App Lifecycle Manager platform logs in Cloud Logging let you monitor operational
events, debug unit provisioning failures, and troubleshoot rollout issues. You
can query these logs in the Logs Explorer and create log-based alerts for
SaaS operations and rollouts in your Google Cloud project.

For details about supported App Lifecycle Manager platform logs, see
[Google Cloud platform logs](https://docs.cloud.google.com/logging/docs/api/platform-logs.md.txt#saas_runtime). For more information about understanding
the data in the platform log tables, see [Log entry structure](https://docs.cloud.google.com/logging/docs/api/platform-logs.md.txt#structure).

## Before you begin

1. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
2. Ensure that billing is enabled for your Google Cloud project.
3. Enable the App Lifecycle Manager API.
4. To get the permissions that you need to view logs and create alerts, ask your administrator to grant you the following Identity and Access Management (IAM) roles on your project:
   - Logs Viewer (`roles/logging.viewer`)
   - Monitoring Editor (`roles/monitoring.editor`)

## Create log-based alerts

To create a log-based alert, you build a query in the Logs Explorer that returns
the log entries for which you want to be alerted, and then you configure the
alert policy.

1. In the Google Cloud console, go to the **Logs Explorer** page.

   [Go to Logs Explorer](https://console.cloud.google.com/logs/query)
2. In the query editor, enter a query to filter for the chosen log entries.
   See the following sections for example queries.

3. Click **Create alert**.

4. Enter a name and description for the alert policy.

5. Configure the alert details, including notification channels.

6. Click **Save alert**.

For detailed instructions, see [Create a log-based alert](https://docs.cloud.google.com/logging/docs/alerting/log-based-alerts.md.txt).

### Example queries for platform logs

This section provides example queries for common alert scenarios based on
App Lifecycle Manager platform logs.

#### SaaS operations alerts

To create an alert for when a unit provisioning operation fails, use the query:

```bash
log_id("saasservicemgmt.googleapis.com/deployment_events") AND
resource.type="saasservicemgmt.googleapis.com/Unit" AND
jsonPayload.unitOperationState="UNIT_OPERATION_STATE_FAILED" AND
jsonPayload.unitOperationType="provision"
```

To create an alert for when a unit provisioning operation reaches a terminal
state (succeeded, failed, or cancelled), use the query:

```bash
log_id("saasservicemgmt.googleapis.com/deployment_events") AND
resource.type="saasservicemgmt.googleapis.com/Unit" AND
(jsonPayload.unitOperationState="UNIT_OPERATION_STATE_SUCCEEDED"
  OR jsonPayload.unitOperationState="UNIT_OPERATION_STATE_FAILED"
  OR jsonPayload.unitOperationState="UNIT_OPERATION_STATE_CANCELLED") AND
jsonPayload.unitOperationType="provision"
```

#### Rollout alerts

To create a log alert for when a rollout enters a `PAUSED` state (because
App Lifecycle Manager has identified the rollout as unhealthy), or a `FAILED`
state, use the query:

```bash
resource.type = "saasservicemgmt.googleapis.com/Rollout"
(jsonPayload.currentState = "PAUSED" AND jsonPayload.reason = "SYSTEM_PAUSE")
  OR
jsonPayload.currentState = "FAILED"
```

To create a metric alert for rollout state metrics when a rollout enters a `PAUSED`
state (because App Lifecycle Manager has identified the rollout as unhealthy),
or a `FAILED` state, use the query:

```bash
saasservicemgmt_googleapis_com:rollout_state
{state_name="PAUSED", reason="SYSTEM_PAUSE"}
OR
saasservicemgmt_googleapis_com:rollout_state{state_name="FAILED"}
```

## What's next

- For more information about supported App Lifecycle Manager platform logs, see [Google Cloud platform logs](https://docs.cloud.google.com/logging/docs/api/platform-logs.md.txt#saas_runtime).
- To understand the data in platform log tables, see [Log entry structure](https://docs.cloud.google.com/logging/docs/api/platform-logs.md.txt#structure).
- Learn more about [App Lifecycle Manager audit logs](https://docs.cloud.google.com/app-lifecycle-manager/audit-logging.md.txt).