diff --git a/composer.json b/composer.json index 8719ee2..235b449 100644 --- a/composer.json +++ b/composer.json @@ -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", @@ -29,4 +29,4 @@ "phpunit/phpunit": "^9.1.5", "symfony/var-dumper": "^5.1.0" } - } \ No newline at end of file + } diff --git a/src/ExceptionFormatter.php b/src/ExceptionFormatter.php index 8bb7288..25da654 100644 --- a/src/ExceptionFormatter.php +++ b/src/ExceptionFormatter.php @@ -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 */ @@ -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(); @@ -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()) {