|
| 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; |
0 commit comments