You can write logs to Cloud Logging from PHP applications by
using the Cloud Logging library for PHP directly.

<br />

## Before you begin


## Installing the Cloud Logging library for PHP

The Cloud Logging library for PHP provides a simple [PSR-3](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md)
logger implementation for PHP web frameworks.

To write logs from your app, add the Cloud Logging library for PHP to your composer.json:

```php
composer require google/cloud-logging
```

### Creating a PSR-3 logger

Use the following code to create a PSR-3 logger:

```php
use Google\Cloud\Logging\LoggingClient;
```

```php
$logging = new LoggingClient([
    'projectId' => $projectId
]);
$logger = $logging->psrLogger('app');
```

### Enabling batching option

> [!NOTE]
> **Note:** This step is optional.

The PSR-3 logger sends the logs synchronously. This means that
whenever you emit a log, it will add RPC latency to the user
request. Especially if you emit multiple logs in a single request, the
added latency will be significant. You probably want to avoid that.

The following code creates a PSR-3 logger which will batch multiple
logs into one single RPC calls:

```php
$logger = LoggingClient::psrBatchLogger('app');
```

By default, this logger batches the logs within a single process. On
App Engine flexible environment, you can configure the Cloud Logging library for PHP for
even higher throughput and lower latency. For details, see
[Configuring the logging daemon](https://docs.cloud.google.com/logging/docs/setup/php.md.txt#gae-config).

### Configuring the Cloud Logging library for PHP

You can customize the behavior of the Cloud Logging library for PHP. See
[the configuration document](https://docs.cloud.google.com/php/docs/reference/cloud-logging/latest/LoggingClient.md.txt#_Google_Cloud_Logging_LoggingClient____construct__)
for a list of possible configuration options.

## Using the PSR-3 logger

Once the logger is created, you can use the logger in your
application:

```php
$logger->info('Hello World');
$logger->error('Oh no');
```

<br />

For more information on installation, see the
[documentation](https://github.com/googleapis/google-cloud-php/tree/master/Logging) for the
Cloud Logging library for PHP. You can also report issues using the
[issue tracker](https://github.com/googleapis/google-cloud-php/issues).

## Write logs with the Cloud Logging client library

For information on using the Cloud Logging client library for
PHP directly, see [Cloud Logging Client Libraries](https://docs.cloud.google.com/logging/docs/reference/libraries.md.txt).

## Run on Google Cloud

For an application to write logs by using the Cloud Logging library for PHP, the
service account for the underlying resource must have the
[Logs Writer (`roles/logging.logWriter`)](https://docs.cloud.google.com/logging/docs/access-control.md.txt#permissions_and_roles) [IAM](https://docs.cloud.google.com/iam/docs/understanding-roles) role.
Most Google Cloud environments automatically configure the default
service account to have this role.

### App Engine

Cloud Logging is automatically enabled for App Engine, and your app's
[default service account](https://docs.cloud.google.com/appengine/docs/standard/php7/service-account) has
the IAM permissions by default to write log entries.

For more information, see [Writing and viewing
logs](https://docs.cloud.google.com/appengine/docs/standard/php7/writing-application-logs).


However, we recommend that you use the PSR-3 logger which automatically
adds metadata to your logs so that your application logs are
correlated to the request logs.

#### Configuring the daemon for App Engine flexible environment

In the App Engine flexible environment, you can configure the
Cloud Logging library for PHP to use an external daemon program to maximize the
log throughput and minimize the app's latency. To enable this feature,
add the following line to `runtime_config` section in `app.yaml`:

```php
enable_stackdriver_integration: true
```

### Google Kubernetes Engine (GKE)

[GKE](https://docs.cloud.google.com/kubernetes-engine/docs.md.txt) automatically grants the default service account
the [Logs Writer (`roles/logging.logWriter`)](https://docs.cloud.google.com/logging/docs/access-control.md.txt#permissions_and_roles) IAM role.
If you use
[Workload Identity Federation for GKE](https://docs.cloud.google.com/kubernetes-engine/docs/concepts/workload-identity.md.txt)
with this default service account to let workloads access specific
Google Cloud APIs, then no additional configuration is required.
However, if you use Workload Identity Federation for GKE with a
custom IAM service account, then ensure that the custom service
account has the role of Logs Writer (`roles/logging.logWriter`).

If needed, you can also use the following command to add the `logging.write`
access scope when creating the cluster:

```bash
gcloud container clusters create example-cluster-name \
    --scopes https://www.googleapis.com/auth/logging.write
```

### Compute Engine

> [!NOTE]
> **Note:** To use the Cloud Logging library for PHP on a **Compute Engine** VM instance, you do not need to install the Cloud Logging agent.

When using [Compute Engine](https://docs.cloud.google.com/compute/docs.md.txt) VM instances, add the `cloud-platform`
access scope to each instance. When creating a new instance through the
Google Cloud console, you can do this in the **Identity and API access** section
of the **Create Instance** panel. Use the Compute Engine default service
account or another service account of your choice, and select
**Allow full access to all Cloud APIs** in the **Identity and API access**
section. Whichever service
account you select, ensure that it has been granted the
[Logs Writer role](https://docs.cloud.google.com/logging/docs/access-control.md.txt#permissions_and_roles) in the **IAM \& Admin** section of the
Google Cloud console.

## Run locally and elsewhere

To use the Cloud Logging library for PHP outside of Google Cloud,
including running the library on your own workstation, on your data center's
computers, or on the VM instances of another cloud provider, you must
set up Application Default Credentials (ADC) in your local environment to
authenticate to the Cloud Logging library for PHP.

For more information, see
[Set up ADC for on-premises or another cloud provider](https://docs.cloud.google.com/docs/authentication/set-up-adc-on-premises.md.txt).

## View the logs

In the Google Cloud console, go to the

**Logs Explorer** page:

[Go to **Logs Explorer**](https://console.cloud.google.com/logs/query)

<br />

If you use the search bar to find this page, then select the result whose subheading is
**Logging**.

In the Logs Explorer, you must specify one or more resources, but the resource
selection might not be obvious. Here are some tips to help you get
started:

- If you are deploying your application to App Engine or using the
  App Engine-specific libraries, set your resource to
  **GAE Application**.

- If you are deploying your application on Compute Engine, set the resource
  to **GCE VM Instance**.

- If you are deploying your application on Google Kubernetes Engine, your
  cluster's logging configuration determines the resource type of the log
  entries. For a detailed
  discussion on the Legacy Google Cloud Observability and the Google Cloud Observability
  Kubernetes Monitoring solutions, and how those options affect the
  resource type, see
  [Migrating to Google Cloud Observability Kubernetes Monitoring](https://docs.cloud.google.com/monitoring/kubernetes-engine/migration#stackdriver-options).

- If your application is using the Cloud Logging API directly,
  the resource is dependent on the API and your configuration. For example,
  in your application, you can specify a resource or use a default resource.

- If you don't see any logs in the Logs Explorer, to see all log entries,
  switch to the advanced query mode and use an empty query.

  1. To switch to the advanced query mode, click **menu** (▾) at the top of the Logs Explorer and then select **Convert to advanced filter**.
  2. Clear the content that appears in the filter box.
  3. Click **Submit Filter**.

  You can examine the individual entries to identify your resources.

For additional information, see [Using the Logs Explorer](https://docs.cloud.google.com/logging/docs/view/logs-explorer-interface.md.txt).