How to write log in Magento 2

In order to write the log in Magento 2, using the Monolog Library for the purpose of the logging is compulsory. Monolog, a common PHP logging solution, has various handlers that create sophisticated logging methods. Its handlers include log to database, to files and syslog, email and alerts sending, etc. Each handler has the option of input message processing and propagation stopping or control passing to the next one in the chain. In this article, Mageplaza will point you to work with Monolog’s Logger class at MAGENTO_ROOT/vendor/monolog/monolog/src/Monolog/Logger.php. Two steps are covered to show the instruction clearly.


Magento 2 extensions

Magento 2 extensions

Allow you to achieve more with your online store

Check it out!


2 Steps to write the log in Magento 2

Step 1: Write the custom log in your module

In your module, please use one of the functions in app/code/Mageplaza/HelloWorld/Block/Post.php. In the guide today, you will see some types of logs such as info, alert, notice, critical, error, and debug in the following code. However, remember that all of them are archived in system.log file expecting from the debug log that is in debug.log file

<?php
namespace Mageplaza\HelloWorld\Post;
class Post extends \Magento\Framework\View\Element\Template
{
        protected $_logger;
 
    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Psr\Log\LoggerInterface $logger,
        array $data = []
    )
    {        
        $this->_logger = $logger;
        parent::__construct($context, $data);
    }
    
    public function testLogging() 
    {    
        // monolog's Logger class
        // MAGENTO_ROOT/vendor/monolog/monolog/src/Monolog/Logger.php
        
        // saved in var/log/debug.log
        $this->_logger->debug('debug1234'); 
        //Output: [2017-02-22 04:48:44] main.DEBUG: debug1234 {"is_exception":false} []
        
        $this->_logger->info('info1234'); 
        // Write to default log file: var/log/system.log
        //Output: [2017-02-22 04:52:56] main.INFO: info1234 [] []
        
        $this->_logger->alert('alert1234'); 
        // Write to default log file: var/log/system.log
        //Output: [2017-02-22 04:52:56] main.ALERT: alert1234 [] []
        
        $this->_logger->notice('notice1234'); 
        // Write to default log file: var/log/system.log
        //Output: [2017-02-22 04:52:56] main.NOTICE: notice1234 [] []
        
        // Write to default log file: var/log/system.log
        $this->_logger->error('error1234'); 
        //Output: [2017-02-22 04:52:56] main.ERROR: error1234 [] []
        
         // Write to default log file: var/log/system.log
        $this->_logger->critical('critical1234'); 
        //Output: [2017-02-22 04:52:56] main.CRITICAL: critical1234 [] []
        
        // Adds a log record at an arbitrary level
        $level = 'DEBUG';
        // saved in var/log/debug.log
        $this->_logger->log($level,'debuglog1234', array('msg'=>'123', 'new' => '456')); 
        //Output: [2017-02-22 04:52:56] main.DEBUG: debuglog1234 {"msg":"123","new":"456","is_exception":false} []
        
        // Write to default log file: var/log/system.log
        $level = 'ERROR';
        $this->_logger->log($level,'errorlog1234', array( array('test1'=>'123', 'test2' => '456'), array('a'=>'b') )); 
        //Output: [2017-02-22 04:52:56] main.ERROR: errorlog1234 [{"test1":"123","test2":"456"},{"a":"b"}] []        
        
    }
    
}
?>

Step 2: Flush Cache and check result

Flush Magento cache and check your result.

If you got this error message: Exception printing is disabled by default for security reasons, this topic may help.

That is it! I believe you could have completed the process by now. If that you have any queries about the article or any questions in general, use the comment section below!

Image Description
With over a decade of experience crafting innovative tech solutions for ecommerce businesses built on Magento, Jacker is the mastermind behind our secure and well-functioned extensions. With his expertise in building user-friendly interfaces and robust back-end systems, Mageplaza was able to deliver exceptional Magento solutions and services for over 122K+ customers around the world.

People also searched for

  • magento 2 logger
  • magento 2 log
  • magento 2 write log
  • magento2 log
  • logger magento 2
  • magento write to log
  • log in magento 2
  • write log magento 2
  • log magento 2
  • add log in magento 2
  • magento 2 add log
  • magento 2 add logger
  • magento 2 log file
  • logger in magento 2
  • magento 2 logging
  • magento 2 write to custom log file
  • magento 2 custom log file
  • magento2 logger
  • magento 2 log to custom file
  • magento 2 write log in custom file
  • magento 2 log to file
  • mage log magento 2
  • magento 2 mage log
  • magento 2 custom logger
  • magento 2 create log file
  • magento 2 debug log
  • how to log in magento 2
  • magento 2 logs
  • magento 2 logger custom file
  • magento 2 write to log
  • 2.3.x, 2.4.x
x