From f9e88b847b73c09f24dd369419f58b38a92cb16c Mon Sep 17 00:00:00 2001 From: Gustavo Freze Date: Sat, 11 Jul 2026 15:03:53 -0300 Subject: [PATCH] refactor: Adopt the http-correlation-id 2.0 API. LogMiddleware holds the plain PSR-3 logger and wraps it per request with CorrelatedLogger when the correlation ID attribute is present, replacing the removed CorrelatedLogger::resolve() and requiring tiny-blocks/http-correlation-id ^2.0. --- composer.json | 2 +- src/LogMiddleware.php | 17 ++++++++++------- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/composer.json b/composer.json index 84faf08..560bf2a 100644 --- a/composer.json +++ b/composer.json @@ -28,7 +28,7 @@ "psr/http-server-middleware": "^1.0", "psr/log": "^3.0", "tiny-blocks/http": "^6.3", - "tiny-blocks/http-correlation-id": "^1.0", + "tiny-blocks/http-correlation-id": "^2.0", "tiny-blocks/time": "^2.3" }, "require-dev": { diff --git a/src/LogMiddleware.php b/src/LogMiddleware.php index 8cc541f..73e397a 100644 --- a/src/LogMiddleware.php +++ b/src/LogMiddleware.php @@ -10,6 +10,8 @@ use Psr\Http\Server\RequestHandlerInterface; use Psr\Log\LoggerInterface; use TinyBlocks\Http\CorrelationId\CorrelatedLogger; +use TinyBlocks\Http\CorrelationId\CorrelationId; +use TinyBlocks\Http\CorrelationId\CorrelationIdMiddleware; use TinyBlocks\Http\Logging\Internal\LogExchange; use TinyBlocks\Time\MonotonicClock; @@ -19,7 +21,7 @@ */ final readonly class LogMiddleware implements MiddlewareInterface { - private function __construct(private MonotonicClock $clock, private CorrelatedLogger $correlatedLogger) + private function __construct(private MonotonicClock $clock, private LoggerInterface $logger) { } @@ -32,7 +34,7 @@ private function __construct(private MonotonicClock $clock, private CorrelatedLo */ public static function build(MonotonicClock $clock, LoggerInterface $logger): LogMiddleware { - return new LogMiddleware(clock: $clock, correlatedLogger: CorrelatedLogger::from(logger: $logger)); + return new LogMiddleware(clock: $clock, logger: $logger); } /** @@ -47,11 +49,12 @@ public static function create(): LogMiddlewareBuilder public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface { - $exchange = LogExchange::start( - clock: $this->clock, - logger: $this->correlatedLogger->resolve(request: $request), - request: $request - ); + $correlationId = $request->getAttribute(CorrelationIdMiddleware::ATTRIBUTE_NAME); + $logger = $correlationId instanceof CorrelationId + ? CorrelatedLogger::from(logger: $this->logger, correlationId: $correlationId) + : $this->logger; + + $exchange = LogExchange::start(clock: $this->clock, logger: $logger, request: $request); $response = $handler->handle($request);