Magento 2 writing messages to a log file
<preference for=”Psr\Log\LoggerInterface” type=”Magento\Framework\Logger\Monolog” />
<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>
<?php
namespace Inchoo\Test\Model;
class Example{
protected $_logger;
public function __construct(
\Psr\Log\LoggerInterface $logger, //log injection
array $data = []
) {
$this->_logger = $logger;
parent::__construct($data);
}
public function someExampleMethod() {
/*
some logic of method
*/
//accessing to logger instance and calling log method
$this->_logger->addDebug('some text or variable');
}
}