You can route platform logs generated by Google Cloud Fraud Defense from
[Cloud Logging](https://docs.cloud.google.com/logging/docs/overview.md.txt) to
[BigQuery](https://docs.cloud.google.com/bigquery/docs/introduction.md.txt) by creating a log sink in
Log Router.

You can export platform logs to BigQuery to retain assessment
data long term, analyze risk scores and reason codes with SQL, and visualize
traffic trends in [Data Studio](https://docs.cloud.google.com/recaptcha/docs/looker.md.txt). Platform logs
in Logging have a
[default retention period](https://docs.cloud.google.com/logging/quotas.md.txt#logs_retention_periods) of 30 days,
which you can configure to be up to 3,650 days.

Fraud Defense supports platform logging for the following
`v1` API operations:

- [`Assessment`](https://docs.cloud.google.com/recaptcha/docs/reference/rest/v1/projects.assessments.md.txt#Assessment) response from [`projects.assessments.create`](https://docs.cloud.google.com/recaptcha/docs/reference/rest/v1/projects.assessments/create.md.txt)
- [`AnnotateAssessmentRequest`](https://docs.cloud.google.com/recaptcha/docs/reference/rpc/google.cloud.recaptchaenterprise.v1.md.txt#google.cloud.recaptchaenterprise.v1.AnnotateAssessmentRequest) request to [`projects.assessments.annotate`](https://docs.cloud.google.com/recaptcha/docs/reference/rest/v1/projects.assessments/annotate.md.txt)

> [!NOTE]
> **Note:** Platform logging is not available for the `v1beta1` and legacy `SiteVerify` APIs. To help protect privacy, platform logs don't include personally identifiable information (PII) supplied for fraud prevention.

## Before you begin

To get the permissions that you need to enable platform logs and route them to
BigQuery, ask your administrator to grant you the following
[Identity and Access Management](https://docs.cloud.google.com/iam/docs/overview.md.txt) roles on your Google Cloud project:

- Enable Fraud Defense platform logging: [reCAPTCHA Enterprise Admin](https://docs.cloud.google.com/iam/docs/understanding-roles#recaptchaenterprise.admin) (`roles/recaptchaenterprise.admin`)
- Create and manage Logging sinks: [Logs Configuration Writer](https://docs.cloud.google.com/logging/docs/access-control.md.txt#logging.configWriter) (`roles/logging.configWriter`)
- Create BigQuery datasets and grant sink write permissions: [BigQuery Data Editor](https://docs.cloud.google.com/bigquery/docs/access-control.md.txt#bigquery.dataEditor) (`roles/bigquery.dataEditor`)

## Enable platform logs

Before routing logs to BigQuery, enable platform logs in
Fraud Defense:

1. In the Google Cloud console, go to the **Fraud Defense** page.

   [Go to Fraud Defense](https://console.cloud.google.com/security/recaptcha)
2. Verify that your project name appears in the resource selector.

   If you don't see your project name, click the resource selector, and then
   select your project.
3. Click **Settings**.

4. In the **Platform logging** card, click **Configure**.

5. In the **Platform logging** pane, click the toggle for
   **Enable assessment logs**.

6. Optional: To export annotation requests, click the toggle next to
   **Enable annotation logs**.

7. Click **Save**.

For more information about managing platform log settings, see
[Working with platform logs](https://docs.cloud.google.com/recaptcha/docs/platform-logging.md.txt).

## Create a BigQuery sink

After you enable platform logs, create a sink in Log Router to export new
reCAPTCHA assessment log entries to BigQuery:

1. In the Google Cloud console, go to the **Log Router** page.

   [Go to Log Router](https://console.cloud.google.com/logs/router)
2. Click **Create sink**.

3. In the **Sink details** section, enter a sink name and an
   optional description, and then click **Next**.

4. To configure the destination dataset, complete the following steps in
   the **Sink destination** section:

   1. In the **Select sink service** menu, select **BigQuery dataset**.
   2. In the **Select BigQuery dataset** menu, select an existing dataset or select **Create new BigQuery
      dataset**.
   3. If you create a new dataset, enter a dataset name, and then click **Create dataset**.
   4. Select **Use partitioned tables** , and then click **Next**.
5. To include all assessment log entries, in the
   **Choose logs to include in sink** section, enter the following inclusion
   filter in the **Build inclusion filter** field:

   ```text
   resource.type="recaptchaenterprise.googleapis.com/Key"
   log_id("recaptchaenterprise.googleapis.com/assessment")
   ```
6. Optional: To include both `assessment` and `annotation` platform logs in the
   sink, enter the following filter instead:

   ```text
   resource.type="recaptchaenterprise.googleapis.com/Key"
   (log_id("recaptchaenterprise.googleapis.com/assessment") OR
     log_id("recaptchaenterprise.googleapis.com/annotation"))
   ```
7. Click **Next**.

8. Optional: Configure exclusion filters if needed, and then click
   **Create sink**.

After the sink is created, Logging displays a confirmation
message and automatically provisions a writer service account with permissions
to write log entries to your BigQuery dataset.

> [!NOTE]
> **Note:** Log sinks export only new log entries received after the sink is created. You can't retroactively export log entries that Logging received before you created the sink.

## Pricing

Exporting platform logs to BigQuery incurs standard charges for
both Logging and BigQuery:

- For log ingestion and routing costs, see [Logging pricing](https://cloud.google.com/stackdriver/pricing#logging-costs).
- For data storage and SQL query costs, see [BigQuery pricing](https://cloud.google.com/bigquery/pricing).

> [!NOTE]
> **Note:** To avoid redundant log billing, ensure that deprecated `DATA_WRITE` audit logs aren't enabled for reCAPTCHA. For more information, see [Fraud Defense audit logging](https://docs.cloud.google.com/recaptcha/docs/audit-logging.md.txt).

## What's next

- [Create dashboards with Data Studio](https://docs.cloud.google.com/recaptcha/docs/looker.md.txt).
- Learn more about [BigQuery storage](https://docs.cloud.google.com/bigquery/docs/storage_overview.md.txt).
- Learn more about [BigQuery analytics](https://docs.cloud.google.com/bigquery/docs/query-overview.md.txt).