From fc77f33d882bddb67436b64ee5c7454e3b0e128f Mon Sep 17 00:00:00 2001 From: abdulmohsen Date: Thu, 16 May 2024 16:47:02 +0300 Subject: [PATCH] Better HTTP logging. --- src/API/Backend/Webhooks.php | 2 +- src/Libs/Initializer.php | 66 +++++++++++++++++++++--------------- 2 files changed, 39 insertions(+), 29 deletions(-) diff --git a/src/API/Backend/Webhooks.php b/src/API/Backend/Webhooks.php index 440993c7..9971913f 100644 --- a/src/API/Backend/Webhooks.php +++ b/src/API/Backend/Webhooks.php @@ -60,7 +60,7 @@ final class Webhooks return api_error('Invalid value for id path parameter.', HTTP_STATUS::HTTP_BAD_REQUEST); } - return $this->process($name, $request)->withHeader('X-Log-Response', '0'); + return $this->process($name, $request); } /** diff --git a/src/Libs/Initializer.php b/src/Libs/Initializer.php index 7a72bcf4..eac23296 100644 --- a/src/Libs/Initializer.php +++ b/src/Libs/Initializer.php @@ -197,39 +197,51 @@ final class Initializer $response = $response->withAddedHeader('X-Application-Version', getAppVersion()); } - if (true === (bool)$response->getHeaderLine('X-Log-Response')) { - if ('OPTIONS' !== $request->getMethod()) { - $this->write($request, Level::Info, $this->formatLog($request, $response)); - } - $response = $response->withoutHeader('X-Log-Response'); + if ('OPTIONS' !== $request->getMethod()) { + $this->write( + $request, + $response->getStatusCode() >= 400 ? Level::Error : Level::Info, + $this->formatLog($request, $response) + ); } - } catch (Throwable $e) { - $httpException = true === ($e instanceof HttpException); - $routeException = true === ($e instanceof RouterHttpException); - $isNormal = true === ($httpException || $routeException); - - $statusCode = $isNormal && ($e->getCode() >= 200 && $e->getCode() <= 499) ? $e->getCode() : 503; + } catch (HttpException|RouterHttpException $e) { + $realStatusCode = ($e instanceof RouterHttpException) ? $e->getStatusCode() : $e->getCode(); + $statusCode = $realStatusCode >= 200 && $realStatusCode <= 499 ? $realStatusCode : 503; $response = addCors( api_error( - message: $isNormal ? "{$e->getCode()}: {$e->getMessage()}" : 'Unable to serve request.', - httpCode: HTTP_STATUS::tryFrom($statusCode) ?? HTTP_STATUS::HTTP_SERVICE_UNAVAILABLE, + message: "{$realStatusCode}: {$e->getMessage()}", + httpCode: HTTP_STATUS::tryFrom($realStatusCode) ?? HTTP_STATUS::HTTP_SERVICE_UNAVAILABLE, ) ); - if (!$isNormal || HTTP_STATUS::HTTP_SERVICE_UNAVAILABLE->value === $statusCode) { - Container::get(LoggerInterface::class)->error( - message: $e->getMessage(), - context: [ - 'kind' => $e::class, - 'file' => $e->getFile(), - 'line' => $e->getLine(), - 'trace' => $e->getTrace(), - ] - ); + if (HTTP_STATUS::HTTP_SERVICE_UNAVAILABLE->value === $statusCode) { + Container::get(LoggerInterface::class)->error($e->getMessage(), [ + 'kind' => $e::class, + 'file' => $e->getFile(), + 'line' => $e->getLine(), + 'trace' => $e->getTrace(), + ]); } - $this->write($request, Level::Info, $this->formatLog($request, $response)); + $this->write( + $request, + $statusCode >= 400 ? Level::Error : Level::Info, + $this->formatLog($request, $response) + ); + } catch (Throwable $e) { + $response = addCors( + api_error(message: 'Unable to serve request.', httpCode: HTTP_STATUS::HTTP_SERVICE_UNAVAILABLE) + ); + + Container::get(LoggerInterface::class)->error($e->getMessage(), [ + 'kind' => $e::class, + 'file' => $e->getFile(), + 'line' => $e->getLine(), + 'trace' => $e->getTrace(), + ]); + + $this->write($request, Level::Error, $this->formatLog($request, $response)); } $emitter($response); @@ -375,9 +387,6 @@ final class Initializer })(); $response = $router->dispatch($realRequest); - if (!$response->hasHeader('X-Log-Response')) { - $response = $response->withHeader('X-Log-Response', '1'); - } return $response->withHeader('Access-Control-Allow-Origin', '*') ->withHeader('Access-Control-Allow-Credentials', 'true'); @@ -560,6 +569,7 @@ final class Initializer $context = array_replace_recursive([ 'request' => [ 'method' => $request->getMethod(), + 'path' => $uri->getPath(), 'id' => ag($params, 'X_REQUEST_ID'), 'ip' => getClientIp($request), 'agent' => ag($params, 'HTTP_USER_AGENT'), @@ -594,7 +604,7 @@ final class Initializer 'uri' => $request->getUri()->getPath(), 'protocol' => 'HTTP/' . $request->getProtocolVersion(), 'status' => $response->getStatusCode(), - 'size' => $response->getBody()->getSize(), + 'size' => $response->getBody()->getSize() ?? 0, 'agent' => ag($request->getServerParams(), 'HTTP_USER_AGENT', '-'), 'refer' => (string)$refer, 'message' => $message ?? '-',