Home / cron / Magento 2 logging

Magento 2 logging

Logging is an important part of every development process. Logs provide insight into system processes of Magento 2 and are a great tool for tracking errors, significant events, exceptions and many more. In order for the logging library to be properly implemented with Magento 2, it must comply with PSR-3 standard.

Magento 2 comes with built-in logging solution based on Monolog Library which will be analyzed further in this article.

Main Magento 2 log class is “Magento\Framework\Logger\Monolog” and is defined in “MAGENTO2_ROOT/app/etc/di.xml”

<preference for=”Psr\Log\LoggerInterface” type=”Magento\Framework\Logger\Monolog”>

As mentioned before Monolog is a powerful logging tool that allows the user to use a wide range of handlers to their benefit, such as logging to files and syslog, sending alerts and e-mails, logging database activity, etc.

Logger class has 8 levels of logs and methods for each of those levels, usage of these methods depends on the type of the message user is logging.

Each of these methods accepts 2 arguments, first one is the message itself ( string) and the second one is an optional array (for example instance of an Exception object)

$this->_logger-> emergency($message, array $context = array());  //saved in var/log/system.log
 
$this->_logger-> alert($message, array $context = array())  //saved in var/log/system.log
 
$this->_logger-> critical($message, array $context = array())  //saved in var/log/system.log
 
$this->_logger-> error($message, array $context = array())  //saved in var/log/system.log
 
$this->_logger-> warning($message, array $context = array())  //saved in  var/log/system.log
 
$this->_logger-> notice($message, array $context = array())  //saved in var/log/system.log
 
$this->_logger-> info($message, array $context = array())  //saved in var/log/system.log
 
$this->_logger-> debug($message, array $context = array())  //saved in var/log/debug.log (does not work in production mode)

There is another method that can be used to log something with an arbitrary level passed as  first parameter

$this->_logger-> log($level, $message, array $context = array())

All exceptions, no matter what log level is used, are saved in var/log/exception.log

Using logger class

Magento 2 uses dependency injection so in order to use logger in our class we must pass it in our class constructor.

Here is an example of how to use logger in a custom class to log an exception.

class Example
{
     private $logger;
 
     public function __construct(\Psr\Log\LoggerInterface $logger) {
         $this->logger = $logger;
     }
 
     public function exampleMethod()
     {
         try {
             //do something
         } catch (\Exception $e) {
             $this->logger->critical('Error message', ['exception' => $e]);
         }
     }
 }

 

Note that we are passing \Psr\Log\LoggerInterface in our constructor, as mentioned at the beginning, preference for LoggerInterface is set to Monolog class in di.xml

In some cases it is not necessary to pass logger in constructor as some classes like  “Magento\Framework\View\Element\Template” and “Magento\Framework\Model\AbstractModel” already have $_logger property so if you extend one of these classes logger is already available.

Debug Logging

By default, Magento writes to the debug log only in default and develop mode, but not in production mode.

To change the default value in default and develop mode following command can be used:

1.  bin/magento setup:config:set –enable-debug-logging=true|false

2. bin/magento cache:flush

Database Logging

Another great Magento 2 functionality is logging database activity. By default, database log is saved in var/debug/db.log. Usage of database logging is highly recommended because it allows us to track all database queries and can help us find performance bottlenecks.

Use the following command to enable/disable database logging:

1.  bin/magento dev:query-log:enable|disable

2.  bin/magento cache:flush

Cron Logging

As of Magento 2.3.1 version cron log is more extensive and is now logged in var/log/cron.log instead of var/log/system.log

These were the basics of Magento 2 logging. Even though Magento 2 uses Monolog logger by default other logging solutions can be used by setting preference for LoggerInterface as long as it complies with PSR-3 standard.

There are also many other possibilities when it comes to logging such as creating custom handlers for custom log files, sending emails with log data, sending log records to a Slack account, etc.

In case you feel you need some extra help, we can offer you a detailed custom report based on our technical audit – feel free to get in touch and see what we can do for you!


Note! This article was revised on Aug 26, 2019. The original article was posted in 2015 by Domagoj Potkoc.

The post Magento 2 logging appeared first on Inchoo.

Click here for more informations about top website development services

About admin

Check Also

Schema Markup for eCommerce Websites

Each time someone searches a particular product online, they end up on a SERP that …

Leave a Reply

Your email address will not be published. Required fields are marked *