1. Go to this page and download the library: Download tiny-blocks/http-logging library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
tiny-blocks / http-logging example snippets
declare(strict_types=1);
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Psr7\ServerRequest;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Psr\Log\LoggerInterface;
use TinyBlocks\Http\Logging\LogMiddleware;
# Build the middleware with the default system monotonic clock and a PSR-3 logger.
$middleware = LogMiddleware::create()->withLogger(logger: $logger)->build();
# Process an inbound request through the middleware.
$response = $middleware->process(
new ServerRequest('GET', '/orders'),
new class () implements RequestHandlerInterface {
public function handle(ServerRequestInterface $request): ResponseInterface
{
return new Response(200, [], '{"status":"ok"}');
}
}
);
# The middleware logged a `request` entry before, and a `response` entry after the handler call.
$response->getStatusCode();
declare(strict_types=1);
use TinyBlocks\Http\CorrelationId\CorrelationIdMiddleware;
use TinyBlocks\Http\Logging\LogMiddleware;
# Composes the two middlewares: correlation-id first so the attribute exists by the time log runs.
$correlationId = CorrelationIdMiddleware::create()->build();
$log = LogMiddleware::create()->withLogger(logger: $logger)->build();
# Every `request` and `response` entry emitted by $log will carry `correlation_id` in context.
declare(strict_types=1);
use TinyBlocks\Http\Logging\LogMiddleware;
use TinyBlocks\Time\MonotonicClock;
# Custom monotonic clock that returns a fixed reading on each call.
$clock = new readonly class () implements MonotonicClock {
public function nanoseconds(): int
{
return 0;
}
};
# Build the middleware with the custom clock.
$middleware = LogMiddleware::create()
->withClock(clock: $clock)
->withLogger(logger: $logger)
->build();
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.