# Google Cloud Logging Plugin

The Google Cloud Log plugin enables pushing logs to your Google Cloud project.

<EnterpriseFeature name="Custom logging" />

## Setup

Before you can use this plugin, you will need to create a GCP Service account
that grants your Zuplo API to write logs. Create a new GCP Service account and
give it the **Logs Writer (roles/logging.logWriter)** permission. Create a key
for the service account in JSON format.

After you have downloaded the JSON formatted service account, save it as a
secret environment variable in your Zuplo project.

```ts title="modules/zuplo.runtime.ts"
import {
  RuntimeExtensions,
  GoogleCloudLoggingPlugin,
  environment,
} from "@zuplo/runtime";

export function runtimeInit(runtime: RuntimeExtensions) {
  runtime.addPlugin(
    new GoogleCloudLoggingPlugin({
      logName: "projects/my-project/logs/my-api",
      serviceAccountJson: environment.GCP_SERVICE_ACCOUNT,
      fields: {
        field1: "value1",
        field2: "value2",
      },
    }),
  );
}
```

## Configuration Options

The `GoogleCloudLoggingPlugin` constructor accepts an options object with the
following properties:

- `serviceAccountJson` - (required) The JSON content of your Google Cloud
  service account key
- `logName` - (required) The name of the log in Google Cloud Logging (for
  example, `projects/my-project/logs/my-api`)
- `fields` - (optional) Custom fields to include in each log entry. Can contain
  string, number, or boolean values

### Custom Fields

Any custom fields you want to include in the log entry can be added to the
`fields` property. These values will be appended to every log entry.

## Default Fields

Every log entry will have a `timestamp` and a `jsonPayload` object. The value of
the `jsonPayload` contains the text or objects passed into the log.

Default fields in the `jsonPayload` are:

- `severity` - The log level (for example, `ERROR`, `INFO`, `DEBUG`, `WARN`)
- `requestId` - The UUID of the request (the value of the `zp-rid` header)
- `environmentType` - Where the Zuplo API is running. Values are `edge`,
  `working-copy`, or `local`
- `environmentStage` - The environment stage: `working-copy`, `preview`, or
  `production`
- `atomicCounter` - An atomic counter used to order logs with identical
  timestamps
- `environment` - The environment name of the Zuplo API
- `rayId` - The network provider identifier (for example, Cloudflare Ray ID) of
  the request
