Skip to content

Latest commit

 

History

History
 
 

README.md

Stackdriver Error Reporting

Open in Cloud Shell

quickstart.php and error_reporting.php are simple command-line programs to demonstrate logging exceptions, errors, and PHP fatral errors to Stackdriver Error Reporting.

Installation

  1. To use this sample, you must first enable the Stackdriver Error Reporting API
  2. Next, Install dependencies via Composer:
    1. Run php composer.phar install --ignore-platform-reqs (if composer is installed locally) or composer install --ignore-platform-reqs (if composer is installed globally).
    composer install --ignore-platform-reqs
    1. If the gRPC PHP Extension is enabled for your version of PHP, install your dependencies without the --ignore-platform-reqs flag. Note some samples in error_reporting.php require gRPC.
  3. Create a service account in the Service Account section of the Cloud Console
  4. Download the JSON key file of the service account.
  5. Set GOOGLE_APPLICATION_CREDENTIALS environment variable to point to that file.
    export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service_account_credentials.json

Running quickstart.php

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 Reporting

View Stackdriver Error Reporting in the Cloud Console to see the logged exception.

Running error_reporting.php

Run the sample:

$ php error_reporting.php report YOUR_PROJECT_ID
Reported an error to Stackdriver

For 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