Skip to content

Commit 3a8fcbf

Browse files
author
Takashi Matsuo
authored
Add a sample for batch/async enabled PSR3 logger (GoogleCloudPlatform#551)
* Added a sample for batch enabled PSR3 logger * CS fix * Removed composer.lock for allowing matrix * Skip when the secret is not available
1 parent 884f623 commit 3a8fcbf

File tree

6 files changed

+77
-1930
lines changed

6 files changed

+77
-1930
lines changed

appengine/flexible/logging/app.php

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,22 @@
1515
* limitations under the License.
1616
*/
1717

18-
use Google\Cloud\Core\Logger\AppEngineFlexHandler;
1918
use Google\Cloud\Logging\LoggingClient;
2019
use Silex\Application;
21-
use Silex\Provider\MonologServiceProvider;
2220
use Silex\Provider\TwigServiceProvider;
2321
use Symfony\Component\HttpFoundation\Request;
2422

2523
// create the Silex application
2624
$app = new Application();
27-
$app['project_id'] = getenv('GCLOUD_PROJECT');
25+
$app['project_id'] = getenv('GOOGLE_CLOUD_PROJECT');
2826
// register twig
2927
$app->register(new TwigServiceProvider(), [
3028
'twig.path' => __DIR__
3129
]);
3230

3331
$app->get('/', function () use ($app) {
3432
if (empty($app['project_id'])) {
35-
return 'Set the GCLOUD_PROJECT environment variable to run locally';
33+
return 'Set the GOOGLE_CLOUD_PROJECT environment variable to run locally';
3634
}
3735
$projectId = $app['project_id'];
3836
# [START list_entries]
@@ -52,19 +50,30 @@
5250
$projectId = $app['project_id'];
5351
$text = $request->get('text');
5452
# [START write_log]
53+
# [START creating_psr3_logger]
5554
$logging = new LoggingClient([
5655
'projectId' => $projectId
5756
]);
58-
$logger = $logging->psrLogger('logging-sample');
57+
$logger = $logging->psrLogger('app');
58+
# [END creating_psr3_logger]
5959
$logger->notice($text);
6060
# [END write_log]
6161
return $app->redirect('/');
6262
});
6363

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-
}
64+
$app->get('/async_log', function (Request $request) use ($app) {
65+
$token = $request->query->get('token');
66+
$projectId = $app['project_id'];
67+
$text = $request->get('text');
68+
# [START enabling_batch]
69+
$logger = LoggingClient::psrBatchLogger('app');
70+
# [END enabling_batch]
71+
# [START using_the_logger]
72+
$logger->info('Hello World');
73+
$logger->error('Oh no');
74+
# [END using_the_logger]
75+
$logger->info("Token: $token");
76+
return 'Sent some logs';
77+
});
6978

7079
return $app;

appengine/flexible/logging/app.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@ env: flex
33

44
runtime_config:
55
document_root: .
6+
# [START enabling_stackdriver_integration]
7+
enable_stackdriver_integration: true
8+
# [END enabling_stackdriver_integration]

appengine/flexible/logging/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"require": {
3-
"google/cloud-logging": "^1.2",
3+
"google/cloud-error-reporting": "^0.8",
4+
"google/cloud-logging": "^1.9",
45
"silex/silex": "^2.0",
56
"twig/twig": "^1.29"
67
},

0 commit comments

Comments
 (0)