PHPackages                             philiprehberger/laravel-correlation-id - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Logging &amp; Monitoring](/categories/logging)
4. /
5. philiprehberger/laravel-correlation-id

ActiveLibrary[Logging &amp; Monitoring](/categories/logging)

philiprehberger/laravel-correlation-id
======================================

Laravel middleware that generates or propagates correlation IDs for request tracing with automatic log context injection

v1.0.4(1mo ago)116[1 PRs](https://github.com/philiprehberger/laravel-correlation-id/pulls)MITPHPPHP ^8.2CI passing

Since Mar 6Pushed 1mo agoCompare

[ Source](https://github.com/philiprehberger/laravel-correlation-id)[ Packagist](https://packagist.org/packages/philiprehberger/laravel-correlation-id)[ Docs](https://github.com/philiprehberger/laravel-correlation-id)[ RSS](/packages/philiprehberger-laravel-correlation-id/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (9)Versions (6)Used By (0)

Laravel Correlation ID
======================

[](#laravel-correlation-id)

[![Tests](https://github.com/philiprehberger/laravel-correlation-id/actions/workflows/tests.yml/badge.svg)](https://github.com/philiprehberger/laravel-correlation-id/actions/workflows/tests.yml)[![Latest Version on Packagist](https://camo.githubusercontent.com/ad785233989807580b4f48b791caac0e830c8c76c44e41a37cf2a8091529e60e/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7068696c69707265686265726765722f6c61726176656c2d636f7272656c6174696f6e2d69642e737667)](https://packagist.org/packages/philiprehberger/laravel-correlation-id)[![License](https://camo.githubusercontent.com/8e392187be68807ea0e947b1da30c66a5a11fe1396ef2a5e792073f9523b86ab/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f7068696c69707265686265726765722f6c61726176656c2d636f7272656c6174696f6e2d6964)](LICENSE)

Laravel middleware that generates or propagates correlation IDs for request tracing with automatic log context injection.

Requirements
------------

[](#requirements)

- PHP 8.2+
- Laravel 11 or 12

Installation
------------

[](#installation)

```
composer require philiprehberger/laravel-correlation-id
```

Laravel's auto-discovery registers the service provider automatically.

### Publish the config (optional)

[](#publish-the-config-optional)

```
php artisan vendor:publish --tag=correlation-id-config
```

This copies `config/correlation-id.php` into your application so you can customise it.

Usage
-----

[](#usage)

### Register the Middleware

[](#register-the-middleware)

Add the middleware to your HTTP kernel in `bootstrap/app.php`:

```
use PhilipRehberger\CorrelationId\AddCorrelationId;

->withMiddleware(function (Middleware $middleware) {
    $middleware->prepend(AddCorrelationId::class);
})
```

### Accessing the Correlation ID

[](#accessing-the-correlation-id)

```
// From the request object
$correlationId = $request->attributes->get('correlation_id');

// Via the helper class
use PhilipRehberger\CorrelationId\CorrelationId;

$id = CorrelationId::get();
CorrelationId::set('my-custom-id');
```

### Configuration

[](#configuration)

```
// config/correlation-id.php
return [
    'request_headers' => ['X-Request-Id', 'X-Correlation-ID'],
    'response_header' => 'X-Request-Id',
    'log_context_key' => 'correlation_id',
    'sentry'          => true,
];
```

### How It Works

[](#how-it-works)

1. The middleware inspects incoming request headers in the order defined by `request_headers`.
2. The first non-empty value found is used as-is (propagation path).
3. When no matching header is present, a new UUID v4 is generated.
4. The ID is stored as a request attribute and shared with the log context.
5. After the handler returns, the ID is written to the response header defined by `response_header`.

### Sentry Integration

[](#sentry-integration)

When `sentry/sentry-laravel` is installed and `'sentry' => true`, the middleware sets `correlation_id` as a tag on every Sentry event captured during the request.

API
---

[](#api)

Class / MethodDescription`AddCorrelationId` middlewareGenerates or propagates the correlation ID and injects it into logs and responses`CorrelationId::get()`Read the current correlation ID (`null` if not yet set)`CorrelationId::set(string $id)`Override the correlation ID (useful in tests or CLI commands)`$request->attributes->get('correlation_id')`Read the ID from the current request`X-Request-Id` response headerOutgoing header carrying the correlation ID (configurable)`correlation_id` log context keyKey injected into every `Log::*` call during the request (configurable)Development
-----------

[](#development)

```
composer install
vendor/bin/phpunit
vendor/bin/pint --test
vendor/bin/phpstan analyse
```

License
-------

[](#license)

MIT

###  Health Score

41

—

FairBetter than 89% of packages

Maintenance89

Actively maintained with recent releases

Popularity10

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity50

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~2 days

Total

5

Last Release

56d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/cfd7d24cbbf32400fa13ce0bbe7a31edd2d66a6d4488eafdb3d64c5337bf0435?d=identicon)[philiprehberger](/maintainers/philiprehberger)

---

Top Contributors

[![philiprehberger](https://avatars.githubusercontent.com/u/8218077?v=4)](https://github.com/philiprehberger "philiprehberger (15 commits)")

---

Tags

middlewarelaravelloggingtracingcorrelation-idrequest id

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StyleLaravel Pint

Type Coverage Yes

### Embed Badge

![Health badge](/badges/philiprehberger-laravel-correlation-id/health.svg)

```
[![Health](https://phpackages.com/badges/philiprehberger-laravel-correlation-id/health.svg)](https://phpackages.com/packages/philiprehberger-laravel-correlation-id)
```

###  Alternatives

[sentry/sentry-laravel

Laravel SDK for Sentry (https://sentry.io)

1.3k114.3M154](/packages/sentry-sentry-laravel)[vinelab/tracing-laravel

Distributed tracing for Laravel made easy

80118.7k1](/packages/vinelab-tracing-laravel)[gluedev/laravel-stackdriver

Enables logging, tracing and error reporting to Google Stackdriver for the Laravel framework

4865.6k](/packages/gluedev-laravel-stackdriver)[shaffe/laravel-mail-log-channel

A package to support logging via email in Laravel

1286.2k](/packages/shaffe-laravel-mail-log-channel)[moesif/moesif-laravel

Moesif Collection/Data Ingestion Middleware for Laravel

1065.8k](/packages/moesif-moesif-laravel)[melihovv/laravel-log-viewer

A Laravel log viewer

1231.5k1](/packages/melihovv-laravel-log-viewer)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
