ampersand / magento2-log-correlation-id
ampersand/magento2-log-correlation-id
Magento 2 correlation id for requests and logs
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-Idheader 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
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)
- Change the key name from
amp_correlation_id - Use existing correlation id from request header
Run module installation
php bin/magento setup:upgrade
Uninstall
rm app/etc/ampersand_magento2_log_correlation/di.xml
php bin/magento module:disable Ampersand_LogCorrelationId
How it works
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
- The correlation ID is attached to web responses as
X-Log-Correlation-Idinsrc/HttpResponse/HeaderProvider/LogCorrelationIdHeader.php- REST API requests work a bit differently in Magento and attach the header using
src/Plugin/AddToWebApiResponse.php
- REST API requests work a bit differently in Magento and attach the header using
- Monolog files have the correlation ID added into their context section under the key
amp_correlation_idviasrc/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_titleviaAmpersand\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
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
Add identifier to MySQL queries
Inside app/etc/ampersand_magento2_log_correlation/di.xml you can change to disabled="false"
<type name="Magento\Framework\App\ResourceConnection">
- <plugin name="ampersand_log_correlation_id_db_query_plugin" disabled="true" />
+ <plugin name="ampersand_log_correlation_id_db_query_plugin" disabled="false" />
</type>
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
You can change the monolog/new relic key from amp_correlation_id using app/etc/ampersand_magento2_log_correlation/di.xml
<type name="Ampersand\LogCorrelationId\Service\CorrelationIdentifier">
<arguments>
<argument name="identifierKey" xsi:type="string">your_key_name_here</argument>
</arguments>
</type>
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
<type name="Ampersand\LogCorrelationId\Service\CorrelationIdentifier">
<arguments>
<argument name="headerInput" xsi:type="string">X-Your-Header-Here</argument>
</arguments>
</type>
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"
< X-Log-Correlation-Id: abc123
$ 2>&1 curl https://your-magento-site.example.com/ -vvv | grep "X-Log-Correlation-Id"
< X-Log-Correlation-Id: cid-61e4194d1bea5
Custom Loggers
By default this module hooks into all vanilla magento loggers.
However third party modules may define additional loggers to write to custom files. If you want the correlation ID added to those logs as well you will need to create a module that depends on both Ampersand_LogCorrelation_Id and the module with the custom logger, you will then have to add the log handler in di.xml like so
<type name="This\Is\Some\Logger">
<arguments>
<argument name="processors" xsi:type="array">
<item name="correlationIdProcessor" xsi:type="array">
<item name="0" xsi:type="object">Ampersand\LogCorrelationId\Processor\MonologCorrelationId</item>
<item name="1" xsi:type="string">addCorrelationId</item>
</item>
</argument>
</arguments>
</type>
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
No changelog yet
The vendor hasn't published a changelog. Tagged releases appear in the Versions tab.
| Version | Stability | QA Status | Compatibility | Released |
|---|---|---|---|---|
| 2.0.1 | stable | Fail | Not compatible Details | 2025-10-06 07:03:40 |
| 2.0.0 | stable | Not tested | Not yet tested Details | 2025-03-11 13:21:48 |
| 1.4.0 | stable | Not tested | Not yet tested Details | 2023-10-27 07:54:59 |
| 1.3.2 | stable | Not tested | Not yet tested Details | 2023-08-23 09:29:45 |
| 1.3.1 | stable | Not tested | Not yet tested Details | 2022-10-24 09:33:54 |
| 1.3.0 | stable | Not tested | Not yet tested Details | 2022-10-18 11:22:39 |
| 1.2.5 | stable | Not tested | Not yet tested Details | 2022-09-01 10:05:04 |
| 1.2.4 | stable | Not tested | Not yet tested Details | 2022-09-01 09:41:36 |
| 1.2.3 | stable | Not tested | Not yet tested Details | 2022-04-22 13:29:20 |
| 1.2.2 | stable | Not tested | Not yet tested Details | 2022-01-31 14:03:04 |
| 1.2.1 | stable | Not tested | Not yet tested Details | 2022-01-27 12:11:59 |
| 1.2.0 | stable | Not tested | Not yet tested Details | 2022-01-27 11:08:21 |
| 1.1.1 | stable | Not tested | Not yet tested Details | 2022-01-20 12:31:54 |
| 1.1.0 | stable | Not tested | Not yet tested Details | 2022-01-17 13:27:38 |
| 1.0.1 | stable | Not tested | Not yet tested Details | 2022-01-13 16:33:35 |
| 1.0.0 | stable | Not tested | Not yet tested Details | 2022-01-13 11:26:08 |
Requires 3
| Package | Constraint |
|---|---|
| php | >=7.3.0 |
| magento/framework | * |
| monolog/monolog | ^3.6 |
Requires-dev 7
| Package | Constraint |
|---|---|
| ampersand/magento-docker-test-instance | ^0.2 |
| bitexpert/phpstan-magento | ^0.11 |
| friendsofphp/php-cs-fixer | ^3.4 |
| magento/magento-coding-standard | ^38.0 |
| magento/magento2-base | * |
| phpstan/phpstan | ^1.5 |
| phpunit/phpunit | ^9.5 |
Compatibility
Each Magento release line is installed on its supported PHP versions, then the module is built (DI compilation + static-content deploy) and its unit and integration suites are run. The matrix shows the lines and PHP versions the module is confirmed to install and run on. Code-quality results further down (phpstan, phpcs, …) are reported separately and never affect compatibility.
Code Quality
Advisory checks against the module's source. Static analysis runs once across the whole module; PHPStan re-runs per Magento + PHP version because resolvable symbols differ between releases. These NEVER affect the Compatibility badge — a phpcs finding can't make a module incompatible.
Static analysis
Coding standards (phpcs), mess detection (phpmd), copy-pasted code (cpd), PHP cross-version compatibility, composer.json validity. Each runs once for the whole module.
| Tool | Status | Findings | Summary |
|---|---|---|---|
| PHPCS | Fail | 18 | 8 errors, 10 warnings (ruleset: Magento2) |
| PHPMD | Warning | 12 | 12 rule violations (MissingImport:6, UnusedFormalParameter:3, ErrorControlOperator:1, EmptyCatchBlock:1, NPathComplexity:1) |
| Cpd | Pass | 0 | |
| Composer validate | Info | 2 | valid; 2 advisory notes (composer validate --strict) |
PHPStan
Type-checks the module's PHP against a real Magento install at the configured gate level. Re-runs per Magento and PHP version because resolvable symbols differ between releases. Cell → details modal.
Tests
Unit and integration suites, run for each applicable Magento and PHP version. A test failure speaks to the module's behaviour, not its compatibility with a Magento line, so it is reported here separately and never reddens the compatibility matrix.
Unit tests
Security
Security checks run directly against the module: an audit of its declared dependencies for known vulnerabilities (composer audit) and a scan of its source for malware and web-shell signatures. Each runs once. A malware detection fails the version outright.
No license declared
This package's composer.json doesn't declare a license, homepage, or authors. Check the source repository for terms of use.
More from ampersand
View vendorA script to help identify local overrides and preferences which need to be reviewed when upgrading magento2
Enable DB, debug log, and verbose logging for a specificly defined request.
Turn an existing module into recurring revenue.
If you already maintain a Magento 2 module on GitHub or GitLab, listing it on Packagento takes about five minutes. We mirror your tags, handle distribution signing, and route paid licenses through Stripe Connect, so you can keep shipping the way you already do.