Repository navigation
Expand file tree
/
Copy pathLogMiddleware.php
More file actions
91 lines (79 loc) · 3.35 KB
/
Copy pathLogMiddleware.php
File metadata and controls
91 lines (79 loc) · 3.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
declare(strict_types=1);
namespace TinyBlocks\Http\Logging;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
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\FailureOnlyLogExchange;
use TinyBlocks\Http\Logging\Internal\LogExchange;
use TinyBlocks\Http\Logging\Internal\Paths;
use TinyBlocks\Time\MonotonicClock;
/**
* PSR-15 middleware that logs request and response metadata (method, URI, status, duration) and attaches
* <code>correlation_id</code> to every log entry when a correlation identifier is present on the request.
*/
final readonly class LogMiddleware implements MiddlewareInterface
{
private function __construct(
private MonotonicClock $clock,
private LoggerInterface $logger,
private Paths $ignoredPaths,
private Paths $failureOnlyPaths
) {
}
/**
* Builds a LogMiddleware from a monotonic clock, a logger, and optional path filters.
*
* <p>Paths are compared by exact string equality with the request URI path. A path present in both lists is
* never logged.</p>
*
* @param MonotonicClock $clock The monotonic clock used to measure request duration.
* @param LoggerInterface $logger The logger to use for request and response logging.
* @param array<string> $ignoredPaths The request paths that are never logged.
* @param array<string> $failureOnlyPaths The request paths that are logged only when the response is an error.
* @return LogMiddleware The configured middleware instance.
*/
public static function build(
MonotonicClock $clock,
LoggerInterface $logger,
array $ignoredPaths = [],
array $failureOnlyPaths = []
): LogMiddleware {
return new LogMiddleware(
clock: $clock,
logger: $logger,
ignoredPaths: Paths::from(...$ignoredPaths),
failureOnlyPaths: Paths::from(...$failureOnlyPaths)
);
}
/**
* Creates a LogMiddlewareBuilder for fluent configuration.
*
* @return LogMiddlewareBuilder The builder instance.
*/
public static function create(): LogMiddlewareBuilder
{
return new LogMiddlewareBuilder();
}
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$path = $request->getUri()->getPath();
if ($this->ignoredPaths->contains(path: $path)) {
return $handler->handle($request);
}
$correlationId = $request->getAttribute(CorrelationIdMiddleware::ATTRIBUTE_NAME);
$logger = $correlationId instanceof CorrelationId
? CorrelatedLogger::from(logger: $this->logger, correlationId: $correlationId)
: $this->logger;
$exchange = $this->failureOnlyPaths->contains(path: $path)
? FailureOnlyLogExchange::start(clock: $this->clock, logger: $logger, request: $request)
: LogExchange::start(clock: $this->clock, logger: $logger, request: $request);
$response = $handler->handle($request);
return $exchange->complete(response: $response);
}
}