How to Utilize Error Logger in Magento 2

Bug fix for Magento 2 is an issue for every Magento developers. Sometimes the Magento offers error message is not enough to understand what happened. You can exploit \ zend_Debug :: dump () & die () to exhibit a variable on a web page. Another way to debug is to use logger. It is more professional than using dump () or tie (). Here I will explain you the following guidelines:

  • Logger Interface in Magento 2
  • Which model does execute the logger interface
  • Observe the detail in log folder

Logger Interface in Magento 2

Magento 2 has defined logger interface at \Psr\Log\LoggerInterface. It supports several methods to write a recordslogs in a folder on the Hard Disk. I will illustrate you patterns in this interface.

interface LoggerInterface

{

    /**

     * System is unusable.

     *

     * @param string $message

     * @param array $context

     * @return null

     */

    public function emergency($message, array $context = array());

    /**

     * Action must be taken immediately.

     *

     * Example: whole website down, database occupied, etc. This should

     * set off the SMS alerts and stir you up.

     *

     * @param string $message

     * @param array $context

     * @return null

     */

    public function alert($message, array $context = array());

    /**

     * Critical conditions.

     *

     * Example: Application component unavailable, unexpected exception.

     *

     * @param string $message

     * @param array $context

     * @return null

     */

    public function critical($message, array $context = array());

    /**

     * Runtime mistake that do not need immediate action but must routinely

     * be logged and observed.

     *

     * @param string $message

     * @param array $context

     * @return null

     */

    public function error($message, array $context = array());

    /**

     * Exceptional occurrences that are not errors.

     *

     * Example: Employ of denounced APIs, poor utilize of an API, unwanted things

     * That is not essentially wrong.

     *

     * @param string $message

     * @param array $context

     * @return null

     */

    public function warning($message, array $context = array());

    /**

     * Normal but significant events.

     *

     * @param string $message

     * @param array $context

     * @return null

     */

    public function notice($message, array $context = array());

    /**

     * Interesting events.

     *

     * Example: User logs in, SQL logs.

     *

     * @param string $message

     * @param array $context

     * @return null

     */

    public function info($message, array $context = array());

    /**

     * Detailed debug information.

     *

     * @param string $message

     * @param array $context

     * @return null

     */

    public function debug($message, array $context = array());

    /**

     * Logs with an arbitrary level.

     *

     * @param mixed $level

     * @param string $message

     * @param array $context

     * @return null

     */

    public function log($level, $message, array $context = array());

}

 Which model does execute the logger interface?

  • Magento 2 uses Monolog to execute the \Psr\Log\LoggerInterface. You can get the Monolog at [Magento_Folder]/vendor/monolog
  • Below mentioned are my examples of using logger in Magento 2.
  • You can use $objectManager to get Logger object or state the Logger object in the construct function of your PHP class. For instance, I use $objectManager.
<?php

public function testLogger()

{

    $logger = $this->objectManager->create('\Psr\Log\LoggerInterface');

    /* save log into debug.log */

    $logger->debug('Test Debug Message');

    /* save log into system.log */

    $logger->info('Test Info Message');

    $logger->notice('Test Notice Message');

    $logger->warning('Test Warning Message');

    $logger->error('Test Error Message');

    $logger->critical('Test Critical Message');

    $logger->alert('Test Alert Message');

    $logger->emergency('Test Emergency Message');

    $logger->log(\Monolog\Logger::CRITICAL, 'Test Log Message');

}

Observe the detail in log folder

  • Notice the loggin details in /Var/log/folder
  • I receive a message in the log files

[Magento_Folder]/var/log/debug.log

[2016-08-28 15:09:48] main.DEBUG: Test Debug Message {"is_exception":false} [
<span style="font-weight: 400;">]</span>
  • [Magento_Folder]/var/log/system.log
[2016-08-28 15:09:48] main.INFO: Test Info Message [] []

[2016-08-28 15:09:48] main.NOTICE: Test Notice Message [] []

[2016-08-28 15:09:48] main.WARNING: Test Warning Message [] []

[2016-08-28 15:09:48] main.ERROR: Test Error Message [] []

[2016-08-28 15:09:48] main.CRITICAL: Test Critical Message [] []

[2016-08-28 15:09:48] main.ALERT: Test Alert Message [] []

[2016-08-28 15:09:48] main.EMERGENCY: Test Emergency Message [] []

These are all steps to utilize Error Logger in Magento 2. Hope all you guys can study magento 2 easier with our tutorial.


Posted

in

by

Tags:

Comments

Leave a Reply

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