Skip to content
Open
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
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,21 @@ public function report(Exception $exception)
}
```

You can optionally supply a second argument to `StackdriverExceptionHandler::report` with which you can send additional information to the error reporting.
The [Google Cloud Error Reporting Reference][link-error-reporting-reference] specifies the correct format.

For example, if you want to add information about the deployed revision in which the error occurred, you may use the following:

```
StackdriverExceptionHandler::report($exception, [
'context' => [
'sourceReferences' => [
'revisionId' => 'example'
]
]
]);
```

Log in to Google Cloud Console and you should start seeing logs, traces and errors appear.

### Batch daemon
Expand Down Expand Up @@ -117,4 +132,5 @@ Please see the [license file](LICENSE.md) for more information.
[link-packagist]: https://packagist.org/packages/gluedev/laravel-stackdriver
[link-downloads]: https://packagist.org/packages/gluedev/laravel-stackdriver
[link-author]: https://github.com/diederikvandenb
[link-contributors]: ../../contributors]
[link-contributors]: ../../contributors
[link-error-reporting-reference]: https://cloud.google.com/error-reporting/reference/rest/v1beta1/ErrorContext
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"illuminate/support": ">=5.7",
"google/cloud-error-reporting": "^0.16.2",
"google/cloud-logging": "^1.14",
"opencensus/opencensus": "^0.5.2",
"opencensus/opencensus": "^0.7.0",
"opencensus/opencensus-exporter-stackdriver": "^0.1",
"php": ">=7.1"
},
Expand Down
64 changes: 62 additions & 2 deletions src/StackdriverExceptionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@
use Google\Cloud\Core\Report\SimpleMetadataProvider;
use Google\Cloud\ErrorReporting\Bootstrap;
use Google\Cloud\Logging\LoggingClient;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Request;

class StackdriverExceptionHandler
{
public static function report($exception): void
/**
* @param mixed $exception \Throwable (PHP 7) or \Exception (PHP 5)
* @param array $additionalContext
*/
public static function report($exception, array $additionalContext = []): void
{
if (config('stackdriver.enabled') === false || config('stackdriver.error_reporting.enabled') === false) {
return;
Expand Down Expand Up @@ -40,6 +46,60 @@ public static function report($exception): void
$psrLogger = $logging->psrLogger('error-log', $psrOptions);

Bootstrap::init($psrLogger);
Bootstrap::exceptionHandler($exception);
self::logError($psrLogger, $exception, $additionalContext);
}

private static function logError($psrLogger, $exception, array $additionalContext): void
{
$message = sprintf('PHP Notice: %s', (string) $exception);

$psrLogger->error($message, array_replace_recursive([
'context' => [
'reportLocation' => [
'filePath' => $exception->getFile(),
'lineNumber' => $exception->getLine(),
'functionName' => self::getFunctionNameForReport($exception->getTrace()),
],
'user' => (string) (Auth::user()->id ?? ''),
'httpRequest' => [
'url' => Request::url(),
'method' => Request::method(),
'userAgent' => Request::userAgent(),
'referrer' => Request::server('HTTP_REFERER'),
'remoteIp' => Request::ip(),
],
],
'serviceContext' => [
'service' => $psrLogger->getMetadataProvider()->serviceId(),
'version' => $psrLogger->getMetadataProvider()->versionId(),
],
], $additionalContext));
}

/**
* Copied from Google\Cloud\ErrorReporting\Bootstrap::getFunctionNameForReport as that function is private
*
* Format the function name from a stack trace. This could be a global
* function (function_name), a class function (Class->function), or a static
* function (Class::function).
*
* @param array $trace The stack trace returned from Exception::getTrace()
*/
private static function getFunctionNameForReport(array $trace = null)
{
if (null === $trace) {
return '<unknown function>';
}
if (empty($trace[0]['function'])) {
return '<none>';
}
$functionName = [$trace[0]['function']];
if (isset($trace[0]['type'])) {
$functionName[] = $trace[0]['type'];
}
if (isset($trace[0]['class'])) {
$functionName[] = $trace[0]['class'];
}
return implode('', array_reverse($functionName));
}
}