Skip to content

Overwriting ObjectManager's $sharedResources leads to strange DI behaviour #3835

@adragus-inviqa

Description

@adragus-inviqa

Steps to reproduce

  1. M2 2.0.2
  2. In developer mode (don't think it matters though), inject \Psr\Log\LoggerInterface into any constructor.

Expected result

  1. Logging using \Psr\Log\LoggerInterface should produce lines in system.log, debug.log, exception.log or support_report.log, depending on level etc.

Actual result

  1. Everything gets logged into support_report.log, including exceptions, layout debug info etc.

More info

We've recently upgraded to 2.0.2 and it dawned to me that nothing but a few lines were logged into system.xml, exception.log was empty even though I had one, my debugging logging wasn't working etc.

Investigating the problem, I saw that \Magento\Framework\Logger\Monolog (thus \Monolog\Logger) was instantiated 2 times: firstly, with 2 handlers: system and debug (normal handlers), secondly, with only one handler: Magento\Support\Model\Logger\Handler\Report. I would expect those three handlers to be merged.

I originally thought some error had slipped when upgrading, and the ObjectManager got instantiated twice, as when searching for \Psr\Log\LoggerInterface, the manager couldn't find it, so it created it again, but this time with the second handler.

But that wasn't the case: the manager was instantiated only once. The reason it couldn't find the original logger (with the 2 handlers) was that the $sharedInstances was being replaced:

.

The conclusion is that the original $sharedInstances which contains a logger instance is getting replaced with another instance with different handlers.

Maybe it's something in our app, but it's worth some investigation, maybe? I also find this a little flaky, as between grabbing the original resources and up until the new ones are loaded, various other instances are created, which will eventually be replaced.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions