How to write a message to a log file?

You can write a message to the var/log/debug.log file by the following code:

\Magento\Framework\App\ObjectManager::getInstance()
    ->get('Psr\Log\LoggerInterface')
        ->debug('message')
;

Magento 1 has much more shorter way to do it:

Mage::log('message');

Magento 2 has shorter ways too: many objects have already the _logger property, so you can call inside such objects:

$this->_logger->debug('message');

But it is not the universal solution, because not all objects have the _logger property.

See also:

See also: