Skip to content

Commit d0194dd

Browse files
authored
Adds php72 logging sample (GoogleCloudPlatform#709)
1 parent 0e8c9d1 commit d0194dd

File tree

9 files changed

+1662
-1
lines changed

9 files changed

+1662
-1
lines changed

appengine/php72/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313

1414
* [Implementing `Google Auth`](auth)
1515
* [Connecting to `Cloud SQL`](cloudsql)
16-
* [Enabling `Cloud Error Reporting`](errorreporting)
16+
* [Enabling `Stackdriver Error Reporting`](errorreporting)
17+
* [Using `Stackdriver Logging`](logging)
1718
* [Implementing a `front controller`](front-controller)
1819
* [Making `gRPC` calls](grpc)
1920
* [Using the `Metadata` server](metadata)

appengine/php72/logging/.gcloudignore

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# This file specifies files that are *not* uploaded to Google Cloud Platform
2+
# using gcloud. It follows the same syntax as .gitignore, with the addition of
3+
# "#!include" directives (which insert the entries of the given .gitignore-style
4+
# file at that point).
5+
#
6+
# For more information, run:
7+
# $ gcloud topic gcloudignore
8+
#
9+
.gcloudignore
10+
# If you would like to upload your .git directory, .gitignore file or files
11+
# from your .gitignore file, remove the corresponding line
12+
# below:
13+
.git
14+
.gitignore
15+
16+
# PHP Composer dependencies:
17+
vendor/

appengine/php72/logging/README.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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+

appengine/php72/logging/app.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
runtime: php72

appengine/php72/logging/composer.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"require": {
3+
"google/cloud-logging": "^1.12"
4+
},
5+
"require-dev": {
6+
"google/cloud-tools": "^0.8.5"
7+
}
8+
}

0 commit comments

Comments
 (0)