You can write logs to Stackdriver Logging from PHP applications by using the Stackdriver Logging library for PHP directly.
Before you begin
Sign in to your Google Account.
If you don't already have one, sign up for a new account.
Select or create a GCP project.
Make sure that billing is enabled for your project.
- Enable the Stackdriver Logging API.
Installing the Stackdriver Logging library for PHP
The Stackdriver Logging library for PHP provides a simple PSR-3 logger implementation for PHP web frameworks.
To write logs from your app, add the Stackdriver Logging library for PHP to your composer.json:
composer require google/cloud-logging
Creating a PSR-3 logger
Use the following code to create a PSR-3 logger:
$logging = new LoggingClient([ 'projectId' => $projectId ]); $logger = $logging->psrLogger('app');
Enabling batching option
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:
$logger = LoggingClient::psrBatchLogger('app');
By default, this logger batches the logs within a single process. On App Engine flexible environment, you can configure the Stackdriver Logging library for PHP for even higher throughput and lower latency. For details, see Configuring the logging daemon.
Configuring the Stackdriver Logging library for PHP
You can customize the behavior of the Stackdriver Logging library for PHP. See the configuration document 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:
$logger->info('Hello World'); $logger->error('Oh no');
Using the Cloud Client Library directly
For information on using the Stackdriver Logging Cloud client library for PHP directly, see Stackdriver Logging Client Libraries.
Running on Google Cloud Platform
The Stackdriver Logging library for PHP can be used without needing to explicitly provide credentials.
Stackdriver Logging is automatically enabled for App Engine applications. No additional setup is required.
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
Stackdriver 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
Google Kubernetes Engine
On Google Kubernetes Engine, you must add the
scope when creating the cluster:
gcloud container clusters create example-cluster-name --scopes https://www.googleapis.com/auth/logging.write
When using Compute Engine VM instances, add the
access scope to each instance. When creating a new instance through the
Google Cloud Platform 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 in the IAM & admin section of the
Running locally and elsewhere
To use the Stackdriver Logging library for PHP outside of GCP, you must supply your GCP project ID and appropriate service account credentials directly to the Stackdriver Logging library for PHP. This applies to running the agent on your own workstation, on your data centers' computers, or on the VM instances of another cloud provider. For more information see Obtaining and providing service account credentials manually.
Viewing the logs
After deployment, you can view the logs in the GCP Console Logs Viewer.
For more information, see Viewing Logs.