How write to log in Magento 2?

The following code snippet is the alternative to Mage::log(‘*’) logging method in Magento 1. It’s a good way to log the errors while you are debugging or in the other situations when you need to write it temporary. It saves your time. The code snippet is in one piece, so you can simply paste it almost everywhere in Magento 2 code.

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

The right way to write to log

But there is a better way to write a log. According to Magento recommendations the better way is to include the logger in a class constructor and call it directly (dependency injection method). Bellow is the example of code how to use it properly.

public $logger;

public function __construct(\Psr\Log\LoggerInterface $logger)
{
    $this->logger = $logger;
}
$this->logger->debug('message');
$this->logger->error('message');

8 different log record types

The Psr\Log has different record types called “log levels”. They let us to group and handle the logs by a purpose. Here is the list of all available types in Magento 2 and how to use them:

//System is unusable.
$this->logger->emergency('Something goes wrong');
//Action must be taken immediately. This should trigger the SMS alerts and wake you up.
$this->logger->alert('Entire website down');
//Critical conditions
$this->logger->critical('Application component unavailable');
//Runtime errors that do not require immediate action but should typically be logged and monitored.
$this->logger->error('Unable to send email');
 //Exceptional occurrences that are not errors.
 $this->logger->warning('Use of deprecated APIs');
//Normal but significant events.
$this->logger->notice('Undefined index of name');
//Interesting events.
$this->logger->info('User logs in');
//Detailed debug information. Working only in default and developer magento modes.
 $this->logger->debug('Working here.');

Where my logs are stored?

The directory of log files is always the same MAGE_DIR/var/log/. According to log record type, your log can be written to one of the files bellow. Make sure Magento has permissions to write a file otherwise log files will be without your records. There are few more situations when records can be missed to write to log file, but about that in our next paragraph.

All logs except debug will be saved to:

var/log/system.log

and only debugging logs will be saved to:

var/log/debug.log

Troubleshooting

I could not write error message to log

  • Cache problem – if you have added code snippet to .phtml there is a big chance that you need to clear a cache.
  • File permission – make sure that Magento has write to files permission on the files located in MAGE_DIR/var/log/.
  • Production mode – log records marked as DEBUG will not be written to file in production mode. Try to use different log level like INFO or ALERT.
  • Disabled in configuration – Magento 2 has an options in configuration for enable/disable logging. You can simple check this out here:
    Stores >> Configuration >> Advanced >> Developer >> Debug >> Log to File
    Stores >> Configuration >> Advanced >> Developer >> Syslog >> Log to Syslog
Stores >> Configuration >> Advanced >> Developer

Error after changes

  • If you are editing controller file and Psr\Log was included in controller constructor, it might be that you will get an error. But solution is simple. To fix this error, there is need to run Magento upgrade command.

These all cases are base on my own experience working with Magento 2. If you had some other situation, when write to log just wasn’t working, let me know in the comments bellow. I will be more than happy to append this post with your case.

Leave a Comment