Skip to content

Commit a009bc5

Browse files
authored
adds flex logging sample (GoogleCloudPlatform#281)
1 parent e94d3cd commit a009bc5

File tree

11 files changed

+3029
-0
lines changed

11 files changed

+3029
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Logging and Google App Engine Flexible
2+
3+
This sample application demonstrates how to implement logging from
4+
Google App Engine Flexible Environment.
5+
6+
## Register your application
7+
8+
- Go to
9+
[Google Developers Console](https://console.developers.google.com/project)
10+
and create a new project. This will automatically enable an App
11+
Engine application with the same ID as the project.
12+
13+
## Prerequisites
14+
15+
- Install [`composer`](https://getcomposer.org)
16+
- Install dependencies by running:
17+
18+
```sh
19+
$ composer install
20+
```
21+
22+
## Deploy to App Engine
23+
24+
**Prerequisites**
25+
26+
- Install the [Google Cloud SDK](https://developers.google.com/cloud/sdk/).
27+
28+
**Deploy with gcloud**
29+
30+
```
31+
$ gcloud config set project YOUR_PROJECT_ID
32+
$ gcloud app deploy
33+
```
34+
35+
## Run Locally
36+
37+
- Go to "Credentials" and create a new Service Account.
38+
39+
- Select "Generate new JSON key", then download a new JSON file.
40+
41+
- Set the following environment variables:
42+
43+
- `GOOGLE_APPLICATION_CREDENTIALS`: the file path to the downloaded JSON file.
44+
- `GCLOUD_PROJECT`: Your project ID
45+
46+
- Run `php -S localhost:8000` and go to http://localhost:8000 in your browser.

appengine/flexible/logging/app.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
/**
3+
* Copyright 2015 Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
use Google\Cloud\Logger\AppEngineFlexHandler;
19+
use Google\Cloud\Logging\LoggingClient;
20+
use Silex\Application;
21+
use Silex\Provider\MonologServiceProvider;
22+
use Silex\Provider\TwigServiceProvider;
23+
use Symfony\Component\HttpFoundation\Request;
24+
25+
// create the Silex application
26+
$app = new Application();
27+
$app['project_id'] = getenv('GCLOUD_PROJECT');
28+
// register twig
29+
$app->register(new TwigServiceProvider(), [
30+
'twig.path' => __DIR__
31+
]);
32+
33+
$app->get('/', function () use ($app) {
34+
if (empty($app['project_id'])) {
35+
return 'Set the GCLOUD_PROJECT environment variable to run locally';
36+
}
37+
$projectId = $app['project_id'];
38+
# [START list_entries]
39+
$logging = new LoggingClient([
40+
'projectId' => $projectId
41+
]);
42+
$logger = $logging->logger('logging-sample');
43+
$logs = $logger->entries([
44+
'pageSize' => 10,
45+
'orderBy' => 'timestamp desc'
46+
]);
47+
# [END list_entries]
48+
return $app['twig']->render('index.html.twig', ['logs' => $logs]);
49+
});
50+
51+
$app->post('/log', function (Request $request) use ($app) {
52+
$projectId = $app['project_id'];
53+
$text = $request->get('text');
54+
# [START write_log]
55+
$logging = new LoggingClient([
56+
'projectId' => $projectId
57+
]);
58+
$logger = $logging->psrLogger('logging-sample');
59+
$logger->notice($text);
60+
# [END write_log]
61+
return $app->redirect('/');
62+
});
63+
64+
// add AppEngineFlexHandler on prod
65+
$app->register(new MonologServiceProvider());
66+
if (isset($_SERVER['GAE_VM']) && $_SERVER['GAE_VM'] === 'true') {
67+
$app['monolog.handler'] = new AppEngineFlexHandler();
68+
}
69+
70+
return $app;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
runtime: php
2+
env: flex
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"require": {
3+
"google/cloud": "^0.20",
4+
"silex/silex": "^2.0",
5+
"twig/twig": "^1.29"
6+
},
7+
"require-dev": {
8+
"symfony/browser-kit": "^3.2",
9+
"google/cloud-tools": "^0.6.1"
10+
}
11+
}

0 commit comments

Comments
 (0)