Skip to content

Cannot Inject Custom Handler to Magento\Framework\Logger\Monolog #2234

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
jksimoniii opened this issue Nov 2, 2015 · 5 comments
Closed

Cannot Inject Custom Handler to Magento\Framework\Logger\Monolog #2234

jksimoniii opened this issue Nov 2, 2015 · 5 comments

Comments

@jksimoniii
Copy link

I am trying to have my module write log messages to it's own file. I found the following argument injection in app/etc/di.xml. I am trying to inject my own handler in the same fashion, but it does not appear when the constructor fires.

I noticed that _Monolog_ is instantiated after the primary scope of DI is merged, but before the global scope gets merged.

If this is not the route to achieve custom log files, what would be the correct approach?

    <type name="Magento\Framework\Logger\Monolog">
        <arguments>
            <argument name="name" xsi:type="string">main</argument>
            <argument name="handlers"  xsi:type="array">
                <item name="system" xsi:type="object">Magento\Framework\Logger\Handler\System</item>
                <item name="debug" xsi:type="object">Magento\Framework\Logger\Handler\Debug</item>
            </argument>
        </arguments>
    </type>
@piotrekkaminski
Copy link
Contributor

@alankent can you look at this one? you did some research on it earlier

@mttjohnson
Copy link

Adding an additional handler to the list is likely not working currently due to an issue in how the \Monolog\Logger is getting the list of handlers from the ObjectManager.

see #2529

For custom logging you could define your own custom logger with a custom handler. You could specify the custom handler to write to a separate file, and anywhere you inject and use your custom logger it would write to the log file you specify in your custom handler. I haven't seen any way of accomplishing this outside of writing your own custom logger and handler.

@vkublytskyi
Copy link

I've discovered an issue and in my opinion we have to follow next points:

  • Magento DI should not allow to reconfigure items in simple list (indexed array). If something could be changed it has meaning so it should be named. So usage of associative arrays is more correct.
  • Magento\Framework\Logger\Monolog is an adapter for Magento to Monolog library. It uses inheritance instead of composition which is not recommended practice now. However even with such implementation it should support Magento DI configuration and adopt it to Monolog expectation.

Internal ticket MAGETWO-52915 created to implement constructor in Magento\Framework\Logger\Monolog to adopt parameters retrieved from DI for Monolog. @piotrekkaminski please prioritize task

@mttjohnson
Copy link

FYI: The Monolog library was updated to support associative arrays in the Monolog 1.18.0 release. Currently it looks like Magento is requiring Monolog 1.16.0.

Also be aware that if you hand Monolog 1.16.0 a list of handlers as an indexed array then the behavior of logging that we currently see could change as I pointed out in #2529 and duplicate the logging of some messages to both the debug and system handlers because of the default behavior of the handler to allowing messages to "bubble" and be passed to the next handler in the list.

@vzabaznov
Copy link
Contributor

Hi @jksimoniii issue was fixed and delivered

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants