PHPackages                             ampersand/magento2-log-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. ampersand/magento2-log-correlation-id

ActiveMagento2-module[Logging &amp; Monitoring](/categories/logging)

ampersand/magento2-log-correlation-id
=====================================

Magento 2 correlation id for requests and logs

v2.0.1(7mo ago)279.9k↓47.1%3[2 issues](https://github.com/idhlagency/magento2-log-correlation-id/issues)PHPPHP &gt;=7.3.0

Since Jan 13Pushed 7mo ago8 watchersCompare

[ Source](https://github.com/idhlagency/magento2-log-correlation-id)[ Packagist](https://packagist.org/packages/ampersand/magento2-log-correlation-id)[ RSS](/packages/ampersand-magento2-log-correlation-id/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (9)Versions (19)Used By (0)

magento2-log-correlation-id
===========================

[](#magento2-log-correlation-id)

[![Build Status](https://camo.githubusercontent.com/898f4e870ee9d620e20a3602cc730a9e7c3a9d0944bf4c1dcbc827592ee79a27/68747470733a2f2f6170702e7472617669732d63692e636f6d2f416d70657273616e6448512f6d6167656e746f322d6c6f672d636f7272656c6174696f6e2d69642e7376673f746f6b656e3d34447a6a457565594e51775a756b337977586a47266272616e63683d6d61696e)](https://app.travis-ci.com/AmpersandHQ/magento2-log-correlation-id)

Magento 2 log correlation id for PHP requests/processes and magento logs.

This is useful when debugging issues on a production site as high amounts of traffic can cause many logs to be written and identifying which logs belong to a specific failing request can sometimes be difficult.

With this you should easily be able to find all associated logs for a given web request or CLI process.

- From a web request you can look at the `X-Log-Correlation-Id` header then search all your magento log files for the logs corresponding to only that request.
- You can also find the log correlation identifier attached to New Relic transactions.

Install
-------

[](#install)

Composer install the module.

```
composer require ampersand/magento2-log-correlation-id
```

Add the additional dependency injection config necessary to boot early in the application flow

```
mkdir app/etc/ampersand_magento2_log_correlation
cp vendor/ampersand/magento2-log-correlation-id/dev/ampersand_magento2_log_correlation/di.xml app/etc/ampersand_magento2_log_correlation/
```

At this point you can make any configuration changes

- [Add identifier to MySQL queries (disabled by default)](#add-identifier-to-mysql-queries)
- [Change the key name from `amp_correlation_id`](#change-the-key-name-from-amp_correlation_id)
- [Use existing correlation id from request header](#use-existing-correlation-id-from-request-header)

Run module installation

```
php bin/magento setup:upgrade
```

Uninstall
---------

[](#uninstall)

```
rm app/etc/ampersand_magento2_log_correlation/di.xml
php bin/magento module:disable Ampersand_LogCorrelationId
```

How it works
------------

[](#how-it-works)

### Entry point

[](#entry-point)

This module creates a new cache decorator (`src/CacheDecorator/CorrelationIdDecorator.php`).

It needs to be here so that it's constructed immediately after `Magento\Framework\Cache\Frontend\Decorator\Logger` which is the class responsible for instantiating `Magento\Framework\App\Request\Http` and the Logger triggered after.

This is the earliest point in the magento stack where we can get any existing traceId from a request header (for example `cf-request-id`) and have it attached to any logs produced.

This cache decorator initialises the identifier which is immutable for the remainder of the request.

### Exit points

[](#exit-points)

- The correlation ID is attached to web responses as `X-Log-Correlation-Id` in `src/HttpResponse/HeaderProvider/LogCorrelationIdHeader.php`
    - REST API requests work a bit differently in Magento and attach the header using `src/Plugin/AddToWebApiResponse.php`
- Monolog files have the correlation ID added into their context section under the key `amp_correlation_id` via `src/Processor/MonologCorrelationId.php`
- Magento database logs have this identifier added by `src/Plugin/AddToDatabaseLogs.php`
- New Relic has this added as a custom parameter under the key `amp_correlation_id`
- CLI processes have it added as their "title" by using `cli_set_process_title` via `Ampersand\LogCorrelationId\CacheDecorator\CorrelationIdDecorator::setCliProcessTitle`
- MySQL queries have it added as a comment at the end of the query via `Ampersand\LogCorrelationId\Plugin\AddToDatabaseQueries::afterGetConnection`

Example usage
-------------

[](#example-usage)

Firstly you need to expose the header in your logs, this is an example for apache logs

```
Header always note X-Log-Correlation-Id amp_correlation_id
LogFormat "%t %U %{amp_correlation_id}n" examplelogformat
CustomLog "/path/to/var/log/httpd/access_log" examplelogformat
```

If you are using Nginx, that's how you can add the correlation id to the access logs

```
log_format examplelogformat '$time_local  $request $sent_http_x_log_correlation_id'
```

The above configuration would give log output like the following when viewing a page

```
[13/Jan/2021:11:34:37 +0000] /some-cms-page/ cid-61e04d741bf78
```

You could then search for all magento logs pertaining to that request

```
$ grep -ri 61e04d741bf78 ./var/log
./var/log/system.log:[2022-01-13 16:04:14] main.INFO: some_log_entry_61e04d7e2ed03 {"amp_correlation_id":"cid-61e04d741bf78","some":"context"} []
```

If the request was long-running, or had an error it may also be flagged in new relic with the custom parameter `amp_correlation_id`

Configuration and Customisation
-------------------------------

[](#configuration-and-customisation)

### Add identifier to MySQL queries

[](#add-identifier-to-mysql-queries)

Inside `app/etc/ampersand_magento2_log_correlation/di.xml` you can change to `disabled="false"`

```

-
+

```

This will add the correlation identifier to all queries like `SELECT store_group.* FROM store_group /* 'cid-652918943af7b811319570' */ `

### Change the key name from `amp_correlation_id`

[](#change-the-key-name-from-amp_correlation_id)

You can change the monolog/new relic key from `amp_correlation_id` using `app/etc/ampersand_magento2_log_correlation/di.xml`

```

        your_key_name_here

```

### Use existing correlation id from request header

[](#use-existing-correlation-id-from-request-header)

If you want to use an upstream correlation/trace ID you can define one `app/etc/ampersand_magento2_log_correlation/di.xml`

```

        X-Your-Header-Here

```

If this is present on the request magento will use that value for `X-Log-Correlation-Id`, the monolog context, and the New Relic parameter. Otherwise magento will generate one.

For example

```
$ 2>&1 curl  -H 'X-Your-Header-Here: abc123' https://your-magento-site.example.com/ -vvv | grep "X-Log-Correlation-Id"
&1 curl https://your-magento-site.example.com/ -vvv | grep "X-Log-Correlation-Id"

                Ampersand\LogCorrelationId\Processor\MonologCorrelationId
                addCorrelationId

```

This module provides a command to try to help you keep track of the custom loggers in your system

```
$ php bin/magento ampersand:log-correlation-id:list-custom-loggers
Scanning for classes which extend Monolog\Logger
- You need to run 'composer dump-autoload --optimize' for this command to work
- Use this on your local environment to configure your di.xml
- See vendor/ampersand/magento2-log-correlation-id/README.md
--------------------------------------------------------------------------------
Dotdigitalgroup\Email\Logger\Logger
StripeIntegration\Payments\Logger\WebhooksLogger
Yotpo\Yotpo\Model\Logger
--------------------------------------------------------------------------------
DONE
```

###  Health Score

44

—

FairBetter than 92% of packages

Maintenance62

Regular maintenance activity

Popularity35

Limited adoption so far

Community16

Small or concentrated contributor base

Maturity53

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 95.7% 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 ~90 days

Recently: every ~269 days

Total

16

Last Release

225d ago

Major Versions

v1.4.0 → v2.0.02025-03-11

### Community

Maintainers

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

![](https://www.gravatar.com/avatar/248c125feb1acc5c8932c06226dcd5086760736ff5fc9ec69be32a3aeae8a339?d=identicon)[chickenland](/maintainers/chickenland)

---

Top Contributors

[![convenient](https://avatars.githubusercontent.com/u/600190?v=4)](https://github.com/convenient "convenient (66 commits)")[![tr33m4n](https://avatars.githubusercontent.com/u/1771667?v=4)](https://github.com/tr33m4n "tr33m4n (2 commits)")[![onedirect-webdev](https://avatars.githubusercontent.com/u/158306486?v=4)](https://github.com/onedirect-webdev "onedirect-webdev (1 commits)")

---

Tags

log-correlationmagentomagento2

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/ampersand-magento2-log-correlation-id/health.svg)

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

###  Alternatives

[justbetter/magento2-sentry

Magento 2 Logger for Sentry

1851.5M3](/packages/justbetter-magento2-sentry)[graycore/magento2-stdlogging

A Magento 2 module that changes all logging handlers to stdout

2382.6k](/packages/graycore-magento2-stdlogging)[corrivate/magento2-rest-api-logger

Advanced and convenient logging

2146.2k1](/packages/corrivate-magento2-rest-api-logger)[graycore/magento2-graphql-logger

106.6k](/packages/graycore-magento2-graphql-logger)

PHPackages © 2026

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