Skip to content

Add a sample for batch/async enabled PSR3 logger #551

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 19 additions & 10 deletions appengine/flexible/logging/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,22 @@
* limitations under the License.
*/

use Google\Cloud\Core\Logger\AppEngineFlexHandler;
use Google\Cloud\Logging\LoggingClient;
use Silex\Application;
use Silex\Provider\MonologServiceProvider;
use Silex\Provider\TwigServiceProvider;
use Symfony\Component\HttpFoundation\Request;

// create the Silex application
$app = new Application();
$app['project_id'] = getenv('GCLOUD_PROJECT');
$app['project_id'] = getenv('GOOGLE_CLOUD_PROJECT');
// register twig
$app->register(new TwigServiceProvider(), [
'twig.path' => __DIR__
]);

$app->get('/', function () use ($app) {
if (empty($app['project_id'])) {
return 'Set the GCLOUD_PROJECT environment variable to run locally';
return 'Set the GOOGLE_CLOUD_PROJECT environment variable to run locally';
}
$projectId = $app['project_id'];
# [START list_entries]
Expand All @@ -52,19 +50,30 @@
$projectId = $app['project_id'];
$text = $request->get('text');
# [START write_log]
# [START creating_psr3_logger]
$logging = new LoggingClient([
'projectId' => $projectId
]);
$logger = $logging->psrLogger('logging-sample');
$logger = $logging->psrLogger('app');
# [END creating_psr3_logger]
$logger->notice($text);
# [END write_log]
return $app->redirect('/');
});

// add AppEngineFlexHandler on prod
$app->register(new MonologServiceProvider());
if (isset($_SERVER['GAE_VM']) && $_SERVER['GAE_VM'] === 'true') {
$app['monolog.handler'] = new AppEngineFlexHandler();
}
$app->get('/async_log', function (Request $request) use ($app) {
$token = $request->query->get('token');
$projectId = $app['project_id'];
$text = $request->get('text');
# [START enabling_batch]
$logger = LoggingClient::psrBatchLogger('app');
# [END enabling_batch]
# [START using_the_logger]
$logger->info('Hello World');
$logger->error('Oh no');
# [END using_the_logger]
$logger->info("Token: $token");
return 'Sent some logs';
});

return $app;
3 changes: 3 additions & 0 deletions appengine/flexible/logging/app.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ env: flex

runtime_config:
document_root: .
# [START enabling_stackdriver_integration]
enable_stackdriver_integration: true
# [END enabling_stackdriver_integration]
3 changes: 2 additions & 1 deletion appengine/flexible/logging/composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"require": {
"google/cloud-logging": "^1.2",
"google/cloud-error-reporting": "^0.8",
"google/cloud-logging": "^1.9",
"silex/silex": "^2.0",
"twig/twig": "^1.29"
},
Expand Down
Loading