|
| 1 | +# Stackdriver Logging on App Engine Standard for PHP 7.2 |
| 2 | + |
| 3 | +This application demonstrates how to set up logging on App Engine Standard for |
| 4 | +PHP 7.2. It also demonstrates how different log levels are handled. |
| 5 | + |
| 6 | +To set up **logging** in your App Engine PHP 7.2 application, simply follow |
| 7 | +these two steps: |
| 8 | + |
| 9 | +1. Install the Google Cloud Logging client library |
| 10 | + ```sh |
| 11 | + composer require google/cloud-logging |
| 12 | + ``` |
| 13 | +1. Create a [PSR-3][psr3]-compatible logger object |
| 14 | + ```php |
| 15 | + use Google\Cloud\Logging\LoggingClient; |
| 16 | + $logging = new LoggingClient(); |
| 17 | + $logger = $logging->psrLogger('app', ['batchEnabled' => true]); |
| 18 | + ``` |
| 19 | + |
| 20 | +Now you can happily log anything you'd like, and they will show up on |
| 21 | +[console.cloud.google.com/logs](https://console.cloud.google.com/logs): |
| 22 | + |
| 23 | +```php |
| 24 | +// Log messages with varying log levels. |
| 25 | +$logger->info('This will show up as log level INFO'); |
| 26 | +$logger->warning('This will show up as log level WARNING'); |
| 27 | +$logger->error('This will show up as log level ERROR'); |
| 28 | +``` |
| 29 | + |
| 30 | +[psr3]: https://www.php-fig.org/psr/psr-3/ |
| 31 | + |
| 32 | +## Setup the sample |
| 33 | + |
| 34 | +- Install [`composer`](https://getcomposer.org) |
| 35 | +- Install dependencies by running: |
| 36 | + ```sh |
| 37 | + composer install |
| 38 | + ``` |
| 39 | +- Install the [Google Cloud SDK](https://developers.google.com/cloud/sdk/). |
| 40 | + |
| 41 | +## Deploy the sample |
| 42 | + |
| 43 | +### Deploy with `gcloud` |
| 44 | + |
| 45 | +Deploy the samples by doing the following: |
| 46 | + |
| 47 | +``` |
| 48 | +gcloud config set project YOUR_PROJECT_ID |
| 49 | +gcloud app deploy |
| 50 | +gcloud app browse |
| 51 | +``` |
| 52 | +
|
| 53 | +The last command will open `https://{YOUR_PROJECT_ID}.appspot.com/` |
| 54 | +in your browser. Browse to `/` to send in some logs. |
| 55 | +
|
| 56 | +### Run Locally |
| 57 | +
|
| 58 | +Run the sample locally using PHP's build-in web server: |
| 59 | +
|
| 60 | +``` |
| 61 | +# export environemnt variables locally which are set by App Engine when deployed |
| 62 | +export GOOGLE_APPLICATION_CREDENTIALS=/path/to/credentials.json |
| 63 | +export GOOGLE_CLOUD_PROJECT=YOUR_PROJECT_ID |
| 64 | + |
| 65 | +# Run PHP's built-in web server |
| 66 | +php -S localhost:8000 |
| 67 | +``` |
| 68 | +
|
| 69 | +Browse to `localhost:8000` to send in the logs. |
| 70 | +
|
| 71 | +> Note: These logs will show up under the `Global` resource since you are not |
| 72 | +actually sending these from App Engine. |
| 73 | +
|
0 commit comments