Skip to content

Instantly share code, notes, and snippets.

@enumag
Last active January 27, 2022 13:26
Show Gist options
  • Save enumag/baf1a36252b2cf6d3e11e83f825be0ed to your computer and use it in GitHub Desktop.
Save enumag/baf1a36252b2cf6d3e11e83f825be0ed to your computer and use it in GitHub Desktop.
cloud_batch_logging.php
<?php declare(strict_types = 1);
use Google\Cloud\Logging\LoggingClient;
use Google\CloudFunctions\FunctionsFramework;
use GuzzleHttp\Psr7\Response;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
FunctionsFramework::http(
'entrypoint',
function (ServerRequestInterface $request): ResponseInterface
{
$logger = LoggingClient::psrBatchLogger(
'app',
[
'clientConfig' => [
'projectId' => getenv('GC_PROJECT'),
],
'resource' => [
'type' => 'cloud_function',
'labels' => [
'function_name' => getenv('GC_FUNCTION_NAME'),
'region' => getenv('GC_REGION'),
],
],
]
);
try {
// Do your cloud function logic here and use $logger for logging.
$logger->debug('Processing request...');
return new Response(200, [], 'Hello world');
} finally {
$logger->flush();
}
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment