1

In Magento 2, If I have a class named, say

Pulsestorm\Nofrillslayout\Model\Test

and I attempt to inject a non-existant class named

public function __construct(Pulsestorm\Nofrillslayout\Model\Test\Logger $logger)
{
}

Magento will automatically generate a a logger class for me

#File: var/generation/Pulsestorm/Nofrillslayout/Model/Test/Logger.php
<?php
namespace Pulsestorm\Nofrillslayout\Model\Test;

/**
 * Logger class for @see \Pulsestorm\Nofrillslayout\Model\Test
 */
class Logger extends \Pulsestorm\Nofrillslayout\Model\Test
{
    /**
     * Object Manager factory log
     *
     * @var \Magento\Framework\ObjectManager\Factory\Log
     */
    protected $log = null;

    /**
     * Object Manager instance
     *
     * @var \Magento\Framework\ObjectManagerInterface
     */
    protected $subject = null;

    public function __construct($subject, $log)
    {
        $this->log = $log;
        $this->subject = $subject;
    }

    /**
     * Invoke method
     *
     * @param string $methodName
     * @param array $methodArguments
     * @return mixed
     */
    protected function _invoke($methodName, array &$methodArguments)
    {
        $this->log->invoked($this->subject, $methodName);
        $result = call_user_func_array(array($this->subject, $methodName), $methodArguments);
        if ($result === $this->subject) {
            return $this;
        }
        return $result;
    }

    /**
     * Clone subject instance
     */
    public function __clone()
    {
        $this->subject = clone $this->subject;
        $this->log->add($this->subject);
    }

    public function __sleep()
    {
        return array('subject');
    }

    public function __wakeUp()
    {
        $this->log = \Magento\Framework\ObjectManager\Profiler\Log::getInstance();
        $this->log->add($this->subject);
    }
}

However, Magento will also give me the following error.

Does anyone know what these generator logger classes are for and how to use them?

4

A Logger class is created by Magento\Framework\ObjectManager\Profiler\Code\Generator\Logger for each class which is generated or loaded using the ObjectManager. This is done in roughly the same way as in which Interceptor classes (for plugins) are generated. You can find the generator itself at Magento\Framework\Code\Generator and the definition which adds this logger at vendor/magento/magento2-base/app/etc/di.xml at line 643 (clean Magento 2.1.0 codebase):

<item name="logger" xsi:type="string">\Magento\Framework\ObjectManager\Profiler\Code\Generator\Logger</item>

This logger class is used to generate data for the Profiler. It wraps around the class creation and all calls to public methods of your class, thus providing the data which you can see if you enable profiling.

The actual tracking ($this->log->invoked(...) in your example) is done in a singleton instance of Magento\Framework\ObjectManager\Profiler\Log

Information on how to activate and show the profiler can be found in Magento 2 devdocs at http://devdocs.magento.com/guides/v2.1/config-guide/bootstrap/mage-profiler.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.