PSR-15 middleware pair for Slim 4. One middleware handles request tracing with automatic route pattern resolution; an optional second handles error capture.
Installation
composer require logtide/logtide-slim Quick Start
<?php
use LogTide\Slim\LogtideMiddleware;
use LogTide\Slim\LogtideErrorMiddleware;
use Slim\Factory\AppFactory;
// Initialize the SDK
\LogTide\init([
'dsn' => 'https://[email protected]',
'service' => 'slim-api',
'environment' => 'production',
]);
$app = AppFactory::create();
// Add routing middleware first (required for route pattern resolution)
$app->addRoutingMiddleware();
// Add LogTide middleware
$app->add(new LogtideMiddleware());
// Optional: replace Slim's error middleware with LogTide's
$app->add(new LogtideErrorMiddleware());
$app->get('/users/{id}', function ($request, $response, $args) {
// Your route handler
return $response;
});
$app->run(); LogtideMiddleware
The main middleware wraps each HTTP request with a scope and span:
- • Creates an isolated scope per request
- • Extracts
traceparentheader for distributed tracing - • Resolves Slim route patterns (e.g.,
/users/{id}) for span names - • Records HTTP request and response breadcrumbs
- • Captures 5xx responses and exceptions
- • Injects
traceparentinto response
<?php
// Skip health check paths
$app->add(new LogtideMiddleware(
skipPaths: ['/health', '/healthz', '/metrics'],
)); Error Middleware
An optional error middleware that captures all thrown exceptions. Use it as an alternative to Slim's built-in error middleware:
<?php
use LogTide\Slim\LogtideErrorMiddleware;
// This captures exceptions and reports them to LogTide,
// then re-throws so Slim's error handling still works
$app->add(new LogtideErrorMiddleware()); Best Practices
1. Middleware Order
Add
addRoutingMiddleware() before
LogtideMiddleware so route patterns can be resolved
for span names (e.g., HTTP GET /users/{id} instead of HTTP GET /users/123).
2. PSR-15 Compatible
Both middlewares implement
MiddlewareInterface and work with
any PSR-15 compatible dispatcher, not just Slim.