ampersand / magento2-verbose-log-request
为特定定义的请求启用数据库、调试日志和详细日志。
Requires
- php: ^7.4||^8.1
- magento/framework: >=103.0.1
- magento/magento-composer-installer: *
- magento/module-developer: *
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
Suggests
- ampersand/magento2-log-correlation-id: A way to correlate log entries to a request
This package is auto-updated.
Last update: 2024-09-08 15:47:03 UTC
README
摘要
动态更改每个请求的日志级别到DEBUG
。这将为特定定义的请求启用数据库、调试日志和详细日志。
传递一个X-Verbose-Log
头,Magento将为该请求激活通常仅在开发者模式下拥有的日志类型。
这意味着在您在生产环境中调试某些内容时,无需强制切换这些设置即可获取详细信息。这对于高流量网站来说是有益的,因为这会产生大量日志数据,而否则很难缩小您感兴趣的范围。
建议您还安装ampersand/magento2-log-correlation-id以帮助关联您的日志条目。
兼容于Magento 2.4.1及更高版本。
功能
- 将启用数据库查询(和堆栈跟踪)日志并输出到
./var/log/verbose_db.log
- https://experienceleague.adobe.com/docs/commerce-operations/configuration-guide/cli/enable-logging.html#database-logging
- 见
src/Logger/DB/LoggerProxy.php
- Magento
debug
日志通常需要您设置一个标志并刷新缓存,我们可以传递标志以激活它,而无需进行这些步骤来为您的特定请求。- https://experienceleague.adobe.com/docs/commerce-operations/configuration-guide/cli/enable-logging.html#debug-logging
- 见
src/Logger/Handler/Debug.php
- 该模块捆绑了一个缓存装饰器,它将向原始日志提供程序添加
cache_invalidate
、cache_load
和cache_save
信息。- 这使用了一个虚拟类型
Ampersand\VerboseLogRequest\Logger\VerboseDebugLogger
,它调用到->debug()
上的Psr\Log\LoggerInterface
。 - 见
src/CacheDecorator/VerboseLogger.php
- 这使用了一个虚拟类型
- 将redis会话日志级别设置为调试模式(级别7)
- 允许您查看redis锁定获取、锁定等待、僵尸进程等
- https://github.com/colinmollenhour/Cm_RedisSession#configuration-example
示例用法
在您想要调试的系统上运行以下命令以获取当前密钥
$ php bin/magento ampersand:verbose-log-request:get-key
The current key is: d07c0ee76154d48c2974516ef22c1ec0
The current key will expire at: 2022-10-25 09:00:00
向您的目标页面发送带有X-Verbose-Log
头设置为该值的请求
curl -H "X-Verbose-Log: d07c0ee76154d48c2974516ef22c1ec0" https://example.com/your-page/
或
X_VERBOSE_LOG=d07c0ee76154d48c2974516ef22c1ec0 php bin/magento some:command:here
查看所有详细日志文件
12M var/log/debug.log 3.1M var/log/verbose_db.log 4.0K var/log/system.log 4.0K var/log/support_report.log
如果您想进行更复杂的交互,可以使用类似modheader的工具来设置一系列请求的此值,请注意这将输出大量调试数据。
安装
使用Composer安装模块。
composer require ampersand/magento2-verbose-log-request
运行模块安装,这将生成您的ampersand/verbose_log_request/key
在app/etc/config.php
中,提交此更改。
php bin/magento setup:upgrade
更新您的.gitignore
以忽略
app/etc/di.xml_ampersand_magento2_verbose_log_request/di.xml
如果您想给某些管理员用户权限通过管理员面板 -> 账户设置 -> 获取详细日志密钥
获取密钥,创建app/etc/di.xml_ampersand_magento2_verbose_log_request/allowed_emails_di.xml
并定义允许的电子邮件/域名。
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <type name="Ampersand\VerboseLogRequest\Service\Adminhtml\AllowedEmails"> <arguments> <!-- It is recommended to allow specific emails --> <argument name="allowedEmails" xsi:type="array"> <item name="1" xsi:type="string">example@firstexample.com</item> </argument> <!-- There is support for whitelisting a whole domain, but this is less strict than the above --> <argument name="allowedDomains" xsi:type="array"> <item name="1" xsi:type="string">secondexample.com</item> </argument> </arguments> </type> </config>
安全注意事项
因为我们所做的只是写入日志文件,所以最大的“风险”是您的磁盘空间。
但是,由于您需要知道密钥才能触发日志记录,因此它可以限制给您的开发人员,而不会对外部世界开放,除非他们已经可以访问您的文件系统。
日志级别低于“调试”,VerboseDebugLogger
默认情况下,magento 在 developer
模式下启用了 debug
级别日志记录,甚至在某些生产环境中也可能被激活。由于我们想要记录 cache_load
和 cache_save
的信息,这可能会导致您的日志文件在您的开发机器或拥有大量不必要数据的生产环境中迅速填满。
为了确保我们只有在 X-Verbose-Log
请求被标记的情况下才触发这些 额外详尽 的调试级别日志记录,我们提供了一个虚拟类型,您可以将其注入到您的类中。
Ampersand\VerboseLogRequest\Logger\VerboseDebugLogger
将与标准调用 ->debug()
一样写入 ./var/log/debug.log
文件,但它们只有在请求被标记时才会写入。
以这种方式,我们可以在 RFC 5424 中定义的 DEBUG
以下某种日志级别中进行欺骗。它仍然是一个调试日志,但它仅在特定请求时触发,因此与标准调试日志略有不同。
您可以通过定义类似于以下 di.xml
的配置来告诉您的类使用这种调试日志记录,通过用标准 \Psr\Log\LoggerInterface
替换注入它
<type name="Namespace\Module\Your\Class\Here"> <arguments> <argument name="logger" xsi:type="object">Ampersand\VerboseLogRequest\Logger\VerboseDebugLogger</argument> </arguments> </type>
这对于添加热修复日志非常有用
我们都希望它永远都不必要,但如果您有在环境中添加一些热修复日志记录的需求,您可以使用 VerboseDebugLogger
来改进它
注入这个依赖关系(如上所述),您可以在应用程序中放置调试语句,这些语句只有在您请求有问题的环境时才会触发。