Skip to content

Instantly share code, notes, and snippets.

@afeld
Created February 10, 2019 08:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save afeld/b02a57c1089295aa2c982cb8d40bec17 to your computer and use it in GitHub Desktop.
Save afeld/b02a57c1089295aa2c982cb8d40bec17 to your computer and use it in GitHub Desktop.
StackDriver reporting for PHP CodeIgniter
<?php
use Google\Cloud\ErrorReporting\Bootstrap;
use Google\Cloud\Logging\LoggingClient;
use Google\Cloud\Core\Report\SimpleMetadataProvider;
// application/core/MY_Exceptions.php
class MY_Exceptions extends CI_Exceptions {
public function __construct()
{
parent::__construct();
// set up StackDriver exception handling
// https://github.com/GoogleCloudPlatform/php-docs-samples/blob/83cbbeb2ee36a458db4d2dc868cd8e16812fa957/error_reporting/quickstart.php
// TODO not in development
$projectId = getenv('GCLOUD_PROJECT') ?: 'YOUR_PROJECT_ID';
$service = getenv('GAE_SERVICE') ?: 'testservice';
$version = getenv('GAE_VERSION') ?: 'test';
$logging = new LoggingClient([
'projectId' => $projectId,
]);
$metadata = new SimpleMetadataProvider([], $projectId, $service, $version);
$psrLogger = $logging->psrLogger('error-log', [
'metadataProvider' => $metadata,
]);
Bootstrap::init($psrLogger);
}
public function show_exception($exception)
{
Bootstrap::exceptionHandler($exception);
parent::show_exception($exception);
}
public function show_php_error($severity, $message, $filepath, $line)
{
Bootstrap::errorHandler($severity, $message, $filepath, $line);
parent::show_php_error($severity, $message, $filepath, $line);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment