## Tracing GRPC issues

### Before you begin

Be sure to initialize and enable write permissions before writing to your log
file. In the following example, the log file is named `/var/log/grpc.log`:

```bash
touch /var/log/grpc.log
chmod 666 /var/log/grpc.log
```

If you encounter file permissions issues, you might need to preface the
previous commands with `sudo`.

### Logging to a file

To save gRPC logs to a file, add the following lines to your `php.ini` file:

```php
grpc.grpc_verbosity=debug
grpc.grpc_trace=all,-timer_check
grpc.log_filename=/var/log/grpc.log
```

To write logs to `stderr` instead, exclude the last line from the
previous example. For more information about other options, see
[gRPC environment
variables](https://grpc.github.io/grpc/php/md_doc_environment_variables.html).

> [!NOTE]
> **Note:** Enabling gRPC logs can also create many logs that grow large quickly. If you enable logging, be sure to clear the log file regularly with a `cron` job, set up a log file rotation, or turn logging off when finished debugging.

## Error Details

Google Cloud services follow [AIP-193](https://google.aip.dev/193) guidelines
when returning errors. Google Cloud services use a defined set of standard
error payloads, which you can find in
[google.rpc.error_details.proto](https://github.com/googleapis/googleapis/blob/master/google/rpc/error_details.proto).
These cover the most common API errors, such as quota failure and invalid
parameters.

API errors emitted by PHP client libraries contain `message`, `code`, `status`, and `details` fields.
Within the `details` field, additional `google.rpc.ErrorInfo` data appears in
`reason`, `domain`, and `metadata` fields.
The following sample shows an error that might appear if you called the
Translation library with a cloud project before enabling the Translate API:

```
Google\ApiCore\ApiException: {
    "reason": "SERVICE_DISABLED",
    "domain": "googleapis.com",
    "errorInfoMetadata": {
        "consumer": "projects\PROJECT_ID",
        "service": "translate.googleapis.com"
    },
    "message": "Cloud Translation API has not been used in project PROJECT_ID before or it is disabled. Enable it by visiting https:\/\/console.developers.google.com\/apis\/api\/translate.googleapis.com\/overview?project=PROJECT_ID then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.",
    "code": 7,
    "status": "PERMISSION_DENIED",
    "details": [
        {
            "@type": "google.rpc.errorinfo-bin",
            "reason": "SERVICE_DISABLED",
            "domain": "googleapis.com",
            "metadata": {
                "consumer": "projects\PROJECT_ID",
                "service": "translate.googleapis.com"
            }
        },
        {
            "@type": "google.rpc.help-bin",
            "links": [
                {
                    "description": "Google developers console API activation",
                    "url": "https:\/\/console.developers.google.com\/apis\/api\/translate.googleapis.com\/overview?project=PROJECT_ID"
                }
            ]
        }
    ]
}
```

For more information about the general error model for Google APIs, see
[Cloud API Errors](https://cloud.google.com/apis/design/errors).