As you can see, Magento 2 still supports the logging system and it makes our work with it easier and helps monitor website’s functioning. Logging system is a very useful tool in Magento. It is important for developers in the code debugging process, the log files help us determine what issues are there, how the store works, which of the functions do not work correctly. So, In this article we are going to describe the Magneto 2 logging system.
Psr\Log\LoggerInterface is implemented according to RFC 5424 and it contains the methods to log information on all of eight logging levels that the RFC provides:
- Debug – detailed debug information.
- Info – interesting events.
- Notice – normal but significant events.
- Warning – exceptional occurrences that are not errors.
- Error – runtime errors that do not require immediate action but should be typically logged and monitored.
- Critical – critical conditions. Example: application component unavailable, unexpected exception.
- Alert – action must be taken immediately. Example: entire website down, database unavailable, etc.
- Emergency – system is unusable.
Let’s see some example how to use it with Magento 2 !
1) Example of using logger in a model
Here you can see that "SomeModel" receives a "\Psr\Logger\LoggerInterface" object via constructor injection. In a method "doSomething" if some error occurred the information about exception will be written to log by calling a method "critical" ($this->_logger->critical($e);). And logged information is stored on var/log/exception.log.
There are eight type of log method (debug, info, notice, warning, error, critical, alert, emergency) available depending on the importance of a log message.
$this->_logger->debug('debug method called');
$this->_logger->info('Info method called');
$this->_logger->notice('notice method called');
$this->_logger->warning('warning method called');
$this->_logger->error('error method called');
$this->_logger->critical('critical method called');
$this->_logger->alert('alert method called');
$this->_logger->emergency('emergency method called');
2) Example of using logger with ObjectManager
// Get Object Manager Instance
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$logger->debug('debug method called');
$logger->info('Info method called');
$logger->notice('notice method called');
$logger->warning('warning method called');
$logger->error('error method called');
$logger->critical('critical method called');
$logger->alert('alert method called');
$logger->emergency('emergency method called');
You can also print PHP objects and arrays like below :
By default, logged information is stored in the following three files (paths is related to Magento root directory):
- var/log/debug.log – all information that is saved by Psr\Log\LoggerInterface::debug() is stored there.
- var/log/exception.log – all exceptions information is stored there.
- var/log/system.log – information from other logging levels is saved there.
It is normal that the log files usually contain much information related to the website’s functioning. But it takes much space on a hard disk and, also, the logs reviewing process becomes difficult because of their large size. Meanwhile, it can be easily fixed by cleaning the var/log directory in the following way:
Open Command line in folder root of magento and run following command.
I hope this Magento technical note helped you to find the quick overview of Magento 2 logging system and some customization options around it useful. Bookmark it for your future reference. Do comment below if you have any other questions or doubts on Magento 2 logging system.
P.S. Do share this note with your team.
eCommerce Project Manager
Magento Technical Notes