I have a DI configuration for a custom exception handler:

<type name="My\Logger\Handler\ExceptionHandler">
    <arguments>
        <argument name="formatter" xsi:type="object">My\Logger\Formatter\LogstashFormatterVirtual</argument>
    </arguments>
</type>

<type name="Monolog\Formatter\LogstashFormatter">
    <arguments>
        <argument name="applicationName" xsi:type="string">Magento</argument>
    </arguments>
</type>

This works fine in developer mode, but if I run bin/magento setup:di:compile, I get the following error as soon as the exception handler would be instantiated:

Missing required argument $applicationName of Monolog\Formatter\LogstashFormatter.

How can I ensure that compilation includes the Monolog type?

有帮助吗?

解决方案

While I still do not understand why it does not work, I solved it by using a virtual type instead:

<type name="My\Logger\Handler\ExceptionHandler">
    <arguments>
        <argument name="formatter" xsi:type="object">My\Logger\Formatter\LogstashFormatterVirtual</argument>
    </arguments>
</type>

<virtualType name="My\Logger\Formatter\LogstashFormatterVirtual" type="Monolog\Formatter\LogstashFormatter">
    <arguments>
        <argument name="applicationName" xsi:type="string">Magento</argument>
    </arguments>
</virtualType>

其他提示

You may try virtual type instead of type in your XML file.

许可以下: CC-BY-SA归因
scroll top