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
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "dancryer/whoops-stackdriver",
"name": "gear4music/whoops-stackdriver",
"description": "Provides a handler for Whoops that outputs a StackDriver ReportedErrorEvent.",
"type": "library",
"license": "BSD-2-Clause",
Expand Down Expand Up @@ -29,4 +29,4 @@
"phpunit/phpunit": "^9.1.5",
"symfony/var-dumper": "^5.1.0"
}
}
}
23 changes: 17 additions & 6 deletions src/ExceptionFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function __construct(Throwable $throwable, array $serviceContext = [])
}

/**
* Create an approriately formatted log message for a Google Stackdriver Reported Error Event.
* Create an appropriately formatted log message for a Google Stackdriver Reported Error Event.
* @link https://cloud.google.com/error-reporting/docs/formatting-error-messages#json_representation
* @return array
*/
Expand All @@ -36,9 +36,12 @@ public function toArray() : array
'serviceContext' => $this->serviceContext,
'context' => [],
'severity' => 'ERROR',

];

if (defined('GCP_TRACE_ID')) {
$rtn['logging.googleapis.com/trace'] = GCP_TRACE_ID;
}

// Set initial context:
if ($this->throwable instanceof ExceptionWithContext) {
$rtn['context'] = $this->throwable->getContext();
Expand All @@ -60,10 +63,18 @@ public function toArray() : array
// HTTP request specific context:
if (php_sapi_name() !== 'cli') {
// Add HTTP request details to the context array:
$rtn['context']['httpRequest']['method'] = $_SERVER['REQUEST_METHOD'];
$rtn['context']['httpRequest']['url'] = $_SERVER['REQUEST_URI'];
$rtn['context']['httpRequest']['userAgent'] = $_SERVER['HTTP_USER_AGENT'];
$rtn['context']['httpRequest']['remoteIp'] = $_SERVER['REMOTE_ADDR'];
$rtn['context']['httpRequest']['method'] = $_SERVER['REQUEST_METHOD'] ?? 'Unknown';
$rtn['context']['httpRequest']['url'] = sprintf(
'%s://%s%s',
isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https' : 'http',
$_SERVER['HTTP_HOST'] ?? 'Unknown',
$_SERVER['REQUEST_URI'] ?? 'Unknown'
);

$rtn['context']['httpRequest']['userAgent'] = $_SERVER['HTTP_USER_AGENT'] ?? 'Unknown';
$rtn['context']['httpRequest']['remoteIp'] = $_SERVER['REMOTE_ADDR'] ?? 'Unknown';
$rtn['context']['httpRequest']['referrer'] = $_SERVER['HTTP_REFERER'] ?? 'Unknown';
$rtn['context']['httpRequest']['responseStatusCode'] = $_SERVER['REDIRECT_STATUS'] ?? 0;

// Add session details to the context array:
if (PHP_SESSION_ACTIVE === session_status()) {
Expand Down