This directory contains samples for setting up and using Stackdriver Error Reporting for PHP.
quickstart.php
and src/report_error.php
are simple command-line programs to
demonstrate logging exceptions, errors, and PHP fatal errors to
Stackdriver Error Reporting.
- To use this sample, you must first enable the Stackdriver Error Reporting API
- Next, Install dependencies via Composer:
- Run
php composer.phar install
(if composer is installed locally) orcomposer install
(if composer is installed globally).
composer install
- To use the gRPC PHP Extension, which will be more performant than REST/HTTP, install and enable the gRPC extension using PECL:
pecl install grpc
- Run
- Create a service account in the Service Account section of the Cloud Console
- Download the JSON key file of the service account.
- Set
GOOGLE_APPLICATION_CREDENTIALS
environment variable to point to that file.export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service_account_credentials.json
Open quickstart.php
in a text editor and replace the text YOUR_PROJECT_ID
with your Project ID.
Run the samples:
php quickstart.php
Throwing a test exception. You can view the message at https://console.cloud.google.com/errors.
This example registers the Stackdriver exception handler using PHP exception handlers. View Stackdriver Error Reporting in the Cloud Console to see the logged exception.
This sample shows how to report an error by creating a ReportedErrorEvent
. The
ReportedErrorEvent
object gives you more control over how the error appears
and the details associated with it.
Run the sample:
$ php src/report_error.php YOUR_PROJECT_ID "This is a test message"
Reported an exception to Stackdriver
View Stackdriver Error Reporting in the Cloud Console to see the logged exception.