ampersand/magento2-log-correlation-id
用于请求和日志的Magento 2关联ID
Requires
- php: >=7.3.0
- magento/framework: *
Requires (Dev)
- ampersand/magento-docker-test-instance: ^0.1
- bitexpert/phpstan-magento: ^0.11
- friendsofphp/php-cs-fixer: ^3.4
- magento/magento-coding-standard: ^15
- magento/magento2-base: *
- phpstan/phpstan: ^1.5
- phpunit/phpunit: ^9.5
README
Magento 2日志关联ID用于PHP请求/进程和magento日志。
在生产网站上调试问题时非常有用,因为高流量可能导致大量日志被写入,有时难以确定哪些日志属于特定的失败请求。
使用此功能,您应该能够轻松找到与特定Web请求或CLI进程相关联的所有日志。
- 从Web请求中,您可以查看
X-Log-Correlation-Id
头部,然后在您的magento日志文件中搜索与该请求相对应的日志。 - 您还可以找到附加到New Relic事务的日志关联标识符。
安装
使用Composer安装模块。
composer require ampersand/magento2-log-correlation-id
添加必要的依赖注入配置以在应用程序流程中早期启动
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/
在此阶段,您可以进行任何配置更改
运行模块安装
php bin/magento setup:upgrade
卸载
rm app/etc/ampersand_magento2_log_correlation/di.xml php bin/magento module:disable Ampersand_LogCorrelationId
工作原理
入口点
此模块创建了一个新的缓存装饰器(src/CacheDecorator/CorrelationIdDecorator.php
)。
它需要在这里,以便在创建Magento\Framework\Cache\Frontend\Decorator\Logger
之后立即构造,这是负责实例化Magento\Framework\App\Request\Http
和触发后续的Logger的类。
这是magento堆栈中可以获取请求头部中任何现有traceId(例如cf-request-id
)并将其附加到任何产生的日志的最早点。
此缓存装饰器初始化标识符,该标识符在请求剩余部分是不可变的。
出口点
- 关联ID作为
X-Log-Correlation-Id
附加到Web响应中,在src/HttpResponse/HeaderProvider/LogCorrelationIdHeader.php
中。- 在Magento中,REST API请求的工作方式略有不同,使用
src/Plugin/AddToWebApiResponse.php
附加头部。
- 在Magento中,REST API请求的工作方式略有不同,使用
- 通过
src/Processor/MonologCorrelationId.php
,Monolog文件将在其上下文部分(键为amp_correlation_id
)中添加关联ID。 - 通过
src/Plugin/AddToDatabaseLogs.php
,magento数据库日志将添加此标识符。 - New Relic将此添加为自定义参数,键为
amp_correlation_id
。 - 通过使用
cli_set_process_title
并通过Ampersand\LogCorrelationId\CacheDecorator\CorrelationIdDecorator::setCliProcessTitle
,CLI进程将添加“标题”。 - 通过
Ampersand\LogCorrelationId\Plugin\AddToDatabaseQueries::afterGetConnection
,MySQL查询将添加作为查询末尾的注释。
示例用法
首先,您需要将头部暴露在日志中,以下是一个Apache日志的示例
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
如果您使用Nginx,您可以这样将关联ID添加到访问日志中
log_format examplelogformat '$time_local $request $sent_http_x_log_correlation_id'
在查看页面时,上述配置将生成以下日志输出
[13/Jan/2021:11:34:37 +0000] /some-cms-page/ cid-61e04d741bf78
然后您可以搜索与该请求相关的所有magento日志
$ 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"} []
如果请求运行时间较长或发生错误,它还可能在New Relic中使用自定义参数amp_correlation_id
标记
配置和自定义
将标识符添加到MySQL查询中
在app/etc/ampersand_magento2_log_correlation/di.xml
中,您可以将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>
这将在所有查询中添加相关性标识符,例如:SELECT store_group.* FROM store_group /* 'cid-652918943af7b811319570' */
更改键名为amp_correlation_id
您可以使用app/etc/ampersand_magento2_log_correlation/di.xml
从amp_correlation_id
更改monolog/new relic键。
<type name="Ampersand\LogCorrelationId\Service\CorrelationIdentifier"> <arguments> <argument name="identifierKey" xsi:type="string">your_key_name_here</argument> </arguments> </type>
使用请求头部中现有的关联ID
如果您想使用上游的相关性/跟踪ID,可以在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>
如果请求中存在此内容,magento将使用该值作为X-Log-Correlation-Id
、monolog上下文和新Relic参数。否则,magento将生成一个。
例如
$ 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
自定义日志记录器
默认情况下,此模块会连接到所有纯magento日志记录器。
然而,第三方模块可能定义额外的日志记录器以写入自定义文件。如果您想将相关性ID添加到这些日志中,您需要创建一个依赖于Ampersand_LogCorrelation_Id
和自定义日志记录器模块的模块,然后您必须在di.xml
中添加日志处理器,如下所示
<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>
此模块提供了一个命令,以帮助您跟踪系统中的自定义日志记录器。
$ 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