Hello, Magenticians
In our last blog, we discussed How can we get product collection in Magento 2.
Today, we are going to discuss Loggers and how to create custom loggers in our custom module.
Logging Overview
Logs provide visibility into Magento system processes; for example, debugging information that assists you with understanding when an error occurred or what lead to the error.
Default Magento 2 comes with monolog library-based components. But today, we are going to see how we can create a custom log file in our custom module programmatically in Magento 2. So, without assist ado, let’s get begun.
Step 1: Declaration of Chanel and handlers
create file di.xml by following the path
app/code/Webiators/Modulename/etc/di.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd"> <type name="Webiators\Modulename\Logger\Handler"> <arguments> <argument name="filesystem" xsi:type="object">Magento\Framework\Filesystem\Driver\File</argument> </arguments> </type> <type name="Webiators\Modulename\Logger\Logger"> <arguments> <argument name="name" xsi:type="string">customLogHandler</argument> <argument name="handlers" xsi:type="array"> <item name="system" xsi:type="object">Webiators\Modulename\Logger\Handler</item> </argument> </arguments> </type> </config> |
Step 2: create a module.xml file app/code/Webiators/Modulename/etc/di.xml
1 2 3 4 5 6 7 |
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <module name="Webiators_Modulename" setup_version="1.0.0" schema_version="1.0.0"> </module> </config> |
Step 3: create a Handler.php file
app/code/Webiators/Modulename/ Logger\Handler.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php namespace Webiators\Modulename\Logger; use Monolog\Logger; class Handler extends \Magento\Framework\Logger\Handler\Base { protected $loggerType = Logger::INFO; protected $fileName = '/var/log/webiators_modulename_log_file.log'; } |
Step 4: create a Logger.php file
app/code/Webiators/Modulename/ Logger\Logger.php
1 2 3 4 5 6 |
<?php namespace Webiators\Modulename\Logger; class Logger extends \Monolog\Logger { } |
Step 5: Start Logging Data in log file webiators_modulename_log_file.log
app/code/Webiators/Modulename/Controller/Index/Index.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
<?php namespace Webiators\Modulename\Controller\Index; use Magento\Framework\App\Action\Action; use Magento\Framework\App\Action\Context; class Index extends Action { /** * Logging instance * @var \Webiators\Modulename\Logger\Logger */ protected $_logger; /** * @param Context $context * @param \Webiators\Modulename\Logger\Logger $logger */ public function __construct( Context $context, \Webiators\Modulename\Logger\Logger $logger ) { $this->_logger = $logger; parent::__construct($context); } /** * Default customer account page * * @return \Magento\Framework\View\Result\Page */ public function execute() { $paramData = $this->getRequest()->getParams(); $this->_logger->info(print_r($paramData)); // log array Data to webiators_modulename_log_file.log $this->_logger->info("This text is for testing log"); // log string Data to webiators_modulename_log_file.log return $this->resultRedirectFactory->create()->setPath('*/*/*'); } } |
Any doubts about the topic? Feel free to mention them in the Comments section below.