2

I have my helper where I want to implement my override logger. The logger work perfect outside the helper but I can't make it work inside the helper:

namespace Acme\Module\Helper; class Data extends \Magento\Framework\App\Helper\AbstractHelper { protected $_logger; public function __construct( \Acme\Module\Logger\Logger $logger ) { $this->_logger = $logger; } public function setLogger($message, $context = info) { $this->_logger->info("y pues anda daleeeeeeeeeeeeeeeeeeeee"); } 

But looks like the whay i am injecting the logger class it is not the correct way to do it. I just get a 500 error so I think it is a problem in the way i am injecting the class into the helper.

=====================================================================

Edited

I manage to avoid the injection error adding a context in the construct but now my class is not overriding the logger as it should:

class Data extends \Magento\Framework\App\Helper\AbstractHelper { protected $httpHeader; protected $mobileAgent; public function __construct( \Acme\Module\Logger\Logger $logger, \Magento\Framework\App\Helper\Context $context ) { $this->_logger = $logger; parent::__construct($context); } public function setLogger($message, $context = false) { $this->_logger->info("y pues anda ooooooooooo"); } 

I also found out that the context can get the folowing methods in the helper:

$context->getRequest(); // return \Magento\Framework\App\RequestInterface $context->getUrlBuilder(); // return \Magento\Framework\UrlInterface $context->getScopeConfig(); // return \Magento\Framework\App\Config\ScopeConfigInterface $context->getLogger(); // return \Psr\Log\LoggerInterface $context->getEventManager(); // return \Magento\Framework\Event\ManagerInterface $context->getModuleManager(); // return \Magento\Framework\Module\Manager $context->getCacheConfig(); // return \Magento\Framework\Cache\ConfigInterface $context->getHttpHeader(); // return \Magento\Framework\HTTP\Header $context->getRemoteAddress(); // return \Magento\Framework\HTTP\PhpEnvironment\RemoteAddress $context->getUrlEncoder(); // return \Magento\Framework\Url\EncoderInterface $context->getUrlDecoder(); // return \Magento\Framework\Url\DecoderInterface 
2
  • What do you have in error logs? Commented Oct 31, 2017 at 12:09
  • No errors. if i make a comment on the construct all work fine but instead of create a new log file using my own class it is adding the text in the default system.log. I know that my log class work fine in another class like the controller for example. I am missing something in the injection of the class but I don't know what Commented Oct 31, 2017 at 15:41

2 Answers 2

0

Probably because of this line:

public function setLogger($message, $context = info) 

where info is constant. Change it to

public function setLogger($message, $context) 
1
  • The info constant it is there but the error happen anyway if i just take all the parameters away. Or even if i take the constructor and let the function use the _logger directly from the magento monolog\Logger Commented Oct 31, 2017 at 15:43
0

The class injection in the Helper works as any other class the only thing to have in mind it is the context that needs to be related to the helper:

For example:

namespace Acme\Mymodule\Helper; use Psr\Log\LoggerInterface; class Data extends \Magento\Framework\App\Helper\AbstractHelper { protected $logger; public function __construct( LoggerInterface $logger, \Magento\Framework\App\Helper\Context $context ) { $this->logger = $logger; parent::__construct($context); } 

The context will bring this intresting methods:

$context->getRequest(); // return \Magento\Framework\App\RequestInterface $context->getUrlBuilder(); // return \Magento\Framework\UrlInterface $context->getScopeConfig(); // return \Magento\Framework\App\Config\ScopeConfigInterface $context->getLogger(); // return \Psr\Log\LoggerInterface $context->getEventManager(); // return \Magento\Framework\Event\ManagerInterface $context->getModuleManager(); // return \Magento\Framework\Module\Manager $context->getCacheConfig(); // return \Magento\Framework\Cache\ConfigInterface $context->getHttpHeader(); // return \Magento\Framework\HTTP\Header $context->getRemoteAddress(); // return \Magento\Framework\HTTP\PhpEnvironment\RemoteAddress $context->getUrlEncoder(); // return \Magento\Framework\Url\EncoderInterface $context->getUrlDecoder(); // return \Magento\Framework\Url\DecoderInterface 

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.