不可破坏 Magento 2 日志包装器

安装: 184

依赖项: 1

建议者: 0

安全: 0

星标: 0

关注者: 1

分支: 0

开放问题: 0

类型:magento2-module

1.0.0 2023-06-25 06:57 UTC

This package is auto-updated.

Last update: 2024-09-13 17:52:54 UTC


README

介绍

本模块编写目的是为了使使用多个日志记录器将记录写入多个日志文件成为可能。

如果您仅仅安装此模块,则没有任何变化,因为默认的 Magento 日志记录器将被添加为第一个日志记录器。您可以使用以下描述的插件添加更多日志记录器。

用法

代码

以下示例基于使用此基础模块的功能模块。

首先,您需要在模块的 di.xml 中添加一个插件定义。

<type name="Infrangible\Log\Logger\Wrapper">
    <plugin name="module-plugin" type="Infrangible\Module\Plugin\Logging" sortOrder="10" disabled="false"/>
</type>

在此插件中,您可以添加尽可能多的附加日志记录器。在本例中,添加了四个额外的日志记录器。日志记录器本身通过 Magento 的依赖注入包含。每个日志记录器都必须实现 LoggerInterface 接口。

<?php

namespace Infrangible\Module\Plugin;

use Infrangible\Module\Logger\Monolog\ErrorLog;
use Infrangible\Module\Logger\Monolog\Log;
use Infrangible\Module\Logger\Monolog\SuccessLog;
use Infrangible\Log\Logger\Wrapper;

/**
 * @author Andreas Knollmann
 */
class Logging
{
    /** @var Log */
    private $moduleLog;

    /** @var ErrorLog */
    private $moduleErrorLog;

    /** @var SuccessLog */
    private $moduleSuccessLog;

    /**
     * @param Log        $moduleLog
     * @param ErrorLog   $moduleErrorLog
     * @param SuccessLog $moduleSuccessLog
     */
    public function __construct(Log $moduleLog, ErrorLog $moduleErrorLog, SuccessLog $moduleSuccessLog)
    {
        $this->moduleLog = $moduleLog;
        $this->moduleErrorLog = $moduleErrorLog;
        $this->moduleSuccessLog = $moduleSuccessLog;
    }

    /**
     * @param Wrapper $wrapper
     */
    public function afterInitialize(Wrapper $wrapper)
    {
        $wrapper->addLoggers([$this->moduleLog, $this->moduleErrorLog, $this->moduleSuccessLog]);
    }
}

许可证

不可破坏日志在 MIT 许可证下授权 - 有关详细信息,请参阅 LICENSE 文件。