quickstart.php and error_reporting.php are simple command-line programs to demonstrate logging
exceptions, errors, and PHP fatral 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 --ignore-platform-reqs(if composer is installed locally) orcomposer install --ignore-platform-reqs(if composer is installed globally).
composer install --ignore-platform-reqs
- If the gRPC PHP Extension is enabled for your version of PHP,
install your dependencies without the
--ignore-platform-reqsflag. Note some samples inerror_reporting.phprequire 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_CREDENTIALSenvironment 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
Exception logged to Stack Driver Error ReportingView Stackdriver Error Reporting in the Cloud Console to see the logged exception.
Run the sample:
$ php error_reporting.php report YOUR_PROJECT_ID
Reported an error to StackdriverFor an example of how to register the Stackdriver exception handler in your custom application, see src/register_exception_handler.php. You can test this out using the samples:
# Test registering an exception handler and then throwing a PHP Fatal Error
$ php error_reporting.php test-exception-handler YOUR_PROJECT_ID --fatal
Triggering a PHP Fatal Error by eval-ing a syntax error...For more granular control over your error reporting, and better performance, you can use the gRPC
library to throw errors. Follow the instructions to install and enable the
gRPC PHP Extension. Now run the gRPC example in error_reporting.php:
$ php error_reporting.php report-grpc YOUR_PROJECT_ID
Reported an error to Stackdriver