phpro / mage2-module-logger-handler
此模块允许您轻松配置自定义日志文件
4.0.0
2022-05-18 08:43 UTC
Requires
- php: ^7.0|^8.1
- magento/framework: ^100.1.7|^101.0.1|^102.0|^103.0
Requires (Dev)
This package is auto-updated.
Last update: 2024-09-18 13:45:18 UTC
README
Magento 2 日志处理器
此模块允许您轻松配置自定义日志文件。特别适用于构建多个集成,每个集成需要单独的日志文件。
安装
composer require phpro/mage2-module-logger-handler
如何使用
此模块仅是一个基本构建块。您可以在其基础上构建以在项目中创建自定义日志文件。下面您可以找到一个示例实现。
商店配置
要使用自定义日志文件,您可以将以下内容添加到系统配置中。此模块还提供日志级别的源模型。
<!-- system.xml -->
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
<system>
<section id="module" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
<group id="log_type" translate="label" sortOrder="20" showInDefault="1">
<field id="log_file_name" translate="label" type="text" sortOrder="10" showInDefault="1">
<label>Log File Name</label>
</field>
<field id="log_level" translate="label" type="select" sortOrder="20" showInDefault="1">
<label>Log Level</label>
<source_model>Phpro\LoggerHandler\Config\LogLevelsSource</source_model> <!-- Custom source model which is available in this module -->
</field>
</group>
</section>
</system>
</config>
配置类
您需要创建一个实现 LogConfiguration 接口的配置类。此配置类将由日志处理器使用。
以下是一个使用上面定义的商店配置的配置类示例。
<?php
namespace Vendor\Module\Config;
use Phpro\LoggerHandler\Config\LogConfiguration;
use Magento\Framework\App\Config\ScopeConfigInterface;
class SystemConfiguration implements LogConfiguration
{
const XML_LOG_FILE_NAME = 'module/log_type/log_file_name';
const XML_LOG_LEVEL = 'module/log_type/log_level';
const LOG_DIR = 'var' . DIRECTORY_SEPARATOR . 'log' . DIRECTORY_SEPARATOR;
/**
* @var ScopeConfigInterface
*/
private $config;
public function __construct(ScopeConfigInterface $config)
{
$this->config = $config;
}
/**
* This function should return the full path to the log file starting from the magento root
*/
public function getLogFileName(): string
{
return self::LOG_DIR . $this->config->getValue(self::XML_LOG_FILE_NAME);
}
public function getLogLevel(): string
{
return $this->config->getValue(self::XML_LOG_LEVEL);
}
}
虚拟类型
要在一个服务类中使用自定义日志记录器,您需要创建以下虚拟类型。
<!-- 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">
<virtualType name="[vendor_module_logtype]_logger_handler" type="Phpro\LoggerHandler\Logger\Handler">
<arguments>
<argument name="config" xsi:type="object">Vendor\Module\Config\SystemConfiguration</argument> <!-- Configuration class created above -->
</arguments>
</virtualType>
<virtualType name="[vendor_module_logtype]_logger" type="Monolog\Logger">
<arguments>
<argument name="name" xsi:type="string">[module-logtype]-logger</argument> <!-- channel name; will also show in log files -->
<argument name="handlers" xsi:type="array">
<item name="stream" xsi:type="object">[vendor_module_logtype]_logger_handler</item> <!-- refers to the logger handler VirtualType -->
</argument>
</arguments>
</virtualType>
<!-- inject custom logger in service class -->
<type name="Vendor\Module\Service\DoSomething">
<arguments>
<argument name="logger" xsi:type="object">[vendor_module_logtype]_logger</argument> <!-- refers to the logger VirtualType -->
</arguments>
</type>
</config>