In today's topic, Milople is showing how to write log in Magento 2. Sometimes it's a bit tough for the developer to deal with errors. So, at that time, the log can help them to find out the solution.
How To Write Log in Magento 2
There are 3 ways to write log in Magento 2:
1. Using Object Manager
\Magento\Framework\App\ObjectManager::getInstance()->get('Psr\Log\LoggerInterface')->info('Message to print');
2. Write Custom Log
<?php namespace Milople\Hello\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() { $this->_logger->debug('debug1234'); $this->_logger->info('info1234'); $this->_logger->alert('alert1234'); $this->_logger->notice('notice1234'); $this->_logger->error('error1234'); $this->_logger->critical('critical1234'); $level = 'DEBUG'; $this->_logger->log($level,'debuglog1234', array('msg'=>'123', 'new' => '456')); $level = 'ERROR'; $this->_logger->log($level,'errorlog1234', array( array('test1'=>'123', 'test2' => '456'), array('a'=>'b') )); } } ?>
Then, once flush the cache. So, you can get the proper result.
Enjoy coding!
Do let us know of any doubts or queries.
Comment(s)
Validate your login
Sign In
Create New Account