Writing a message to a system log in Magento 2
To create a message to a system log in Magento 2.0, use the code listed below:
1 2 3 4 |
\Magento\Framework\App\ObjectManager::getInstance() ->get('Psr\Log\LoggerInterface') ->debug('message') ; |
In Magento 1, for instance, this code was more simple:
1 |
Mage::log('message'); |
but you can also utilize another approach in case of Magento 2. Since there is a _logger property related to many objects, you can perform the same task by calling inside such objects. Just use the following code:
1 |
$this->_logger->debug('message'); |
Please note that not every object has the _logger property. Thus, this method will not work with all your objects.
More tips from Magento 2 Developer’s Cookbook