Magneto 2 Logging System

Posted on 27 January, 2017

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:

  1. Debug – detailed debug information.
  2. Info – interesting events.
  3. Notice – normal but significant events.
  4. Warning – exceptional occurrences that are not errors.
  5. Error – runtime errors that do not require immediate action but should be typically logged and monitored.
  6. Critical – critical conditions. Example: application component unavailable, unexpected exception.
  7. Alert – action must be taken immediately. Example: entire website down, database unavailable, etc.
  8. Emergency – system is unusable.
 

Let’s see some example how to use it with Magento 2 !

1) Example of using logger in a model

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class SomeModel
{
    private $_logger;
    public function __construct(\Psr\Logger\LoggerInterface $logger)
    {
        $this->_logger = $logger;
    }
    public function doSomething()
    {
        try {
            //do something
        } catch (\Exception $e) {
            $this->_logger->critical($e);
        }
    }
}

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=$objectManager->get('Psr\Log\LoggerInterface');
$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 :

$logger->info(print_r($array, true));

By default, logged information is stored in the following three files (paths is related to Magento root directory):

  1. var/log/debug.log – all information that is saved by Psr\Log\LoggerInterface::debug() is stored there.
  2. var/log/exception.log – all exceptions information is stored there.
  3. 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.

rm -rf var/log/*

 

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.


Mayank Zalavadia , eCommerce Project Manager
Magento Technical Notes

Post Your Review

X

Your Review has been posted

0 Comment(s)