Download the PHP package ampersand/magento2-log-correlation-id without Composer
On this page you can find all versions of the php package ampersand/magento2-log-correlation-id. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download ampersand/magento2-log-correlation-id
More information about ampersand/magento2-log-correlation-id
Files in ampersand/magento2-log-correlation-id
Package magento2-log-correlation-id
Short Description Magento 2 correlation id for requests and logs
License
Informations about the package magento2-log-correlation-id
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
Composer install the module.
Add the additional dependency injection config necessary to boot early in the application flow
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
Uninstall
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-Id
insrc/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_id
viasrc/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
viaAmpersand\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
If you are using Nginx, that's how you can add the correlation id to the access logs
The above configuration would give log output like the following when viewing a page
You could then search for all magento logs pertaining to that request
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"
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
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
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
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
This module provides a command to try to help you keep track of the custom loggers in your system