|
| 1 | +# Stackdriver Error Reporting |
| 2 | + |
| 3 | +`quickstart.php` and `error_reporting.php` are simple command-line programs to demonstrate logging |
| 4 | +exceptions, errors, and PHP fatral errors to Stackdriver Error Reporting. |
| 5 | + |
| 6 | +# Installation |
| 7 | + |
| 8 | +1. To use this sample, you must first [enable the Stackdriver Error Reporting API][0] |
| 9 | +1. Next, **Install dependencies** via [Composer](http://getcomposer.org/doc/00-intro.md): |
| 10 | + 1. Run `php composer.phar install --ignore-platform-reqs` (if composer is installed locally) or `composer install --ignore-platform-reqs` |
| 11 | + (if composer is installed globally). |
| 12 | + ```sh |
| 13 | + composer install --ignore-platform-reqs |
| 14 | + ``` |
| 15 | + 1. If the [gRPC PHP Extension][php_grpc] is enabled for your version of PHP, |
| 16 | + install your dependencies without the `--ignore-platform-reqs` flag. **Note** |
| 17 | + some samples in `error_reporting.php` require gRPC. |
| 18 | +1. Create a service account in the [Service Account section of the Cloud Console][2] |
| 19 | +1. Download the JSON key file of the service account. |
| 20 | +1. Set `GOOGLE_APPLICATION_CREDENTIALS` environment variable to point to that file. |
| 21 | + ```sh |
| 22 | + export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service_account_credentials.json |
| 23 | + ``` |
| 24 | + |
| 25 | +# Running quickstart.php |
| 26 | + |
| 27 | +Open `quickstart.php` in a text editor and replace the text `YOUR_PROJECT_ID` |
| 28 | +with your Project ID. |
| 29 | + |
| 30 | +Run the samples: |
| 31 | + |
| 32 | +```sh |
| 33 | +php quickstart.php |
| 34 | +Exception logged to Stack Driver Error Reporting |
| 35 | +``` |
| 36 | + |
| 37 | +View [Stackdriver Error Reporting][1] in the Cloud Console to see the logged |
| 38 | +exception. |
| 39 | + |
| 40 | +# Running error_reporting.php |
| 41 | + |
| 42 | +Run the sample: |
| 43 | + |
| 44 | +```sh |
| 45 | +$ php error_reporting.php report YOUR_PROJECT_ID |
| 46 | +Reported an error to Stackdriver |
| 47 | +``` |
| 48 | + |
| 49 | +For an example of how to register the Stackdriver exception handler in your custom application, see |
| 50 | +[src/register_exception_handler.php](src/register_exception_handler.php). You can test this out |
| 51 | +using the samples: |
| 52 | + |
| 53 | +```sh |
| 54 | +# Test registering an exception handler and then throwing a PHP Fatal Error |
| 55 | +$ php error_reporting.php test-exception-handler YOUR_PROJECT_ID --fatal |
| 56 | +Triggering a PHP Fatal Error by eval-ing a syntax error... |
| 57 | +``` |
| 58 | + |
| 59 | +For more granular control over your error reporting, and better performance, you can use the gRPC |
| 60 | +library to throw errors. Follow the instructions to install and enable the |
| 61 | +[gRPC PHP Extension][php_grpc]. Now run the gRPC example in `error_reporting.php`: |
| 62 | + |
| 63 | +```sh |
| 64 | +$ php error_reporting.php report-grpc YOUR_PROJECT_ID |
| 65 | +Reported an error to Stackdriver |
| 66 | +``` |
| 67 | + |
| 68 | +[0]: https://console.cloud.google.com/flows/enableapi?apiid=clouderrorreporting.googleapis.com |
| 69 | +[1]: https://console.cloud.google.com/errors |
| 70 | +[2]: https://console.cloud.google.com/iam-admin/serviceaccounts/ |
| 71 | +[php_grpc]: http://cloud.google.com/php/grpc |
0 commit comments