How to create custom log file in module in Magento 2
Magento 2 provides three types of log files debug.log, exception.log, and system.log. Based on your need you can log the data in any of these log files. Below is a code snippet of how to write a log in Magento 2. If you want to log data into your custom log file then you need to create a Logger class first.
Use Default Log File in Custom Module in Magento 2
<?php namespace Codextblog\Test\Block; class Test 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 LogData() { // saved data in var/log/debug.log $this->_logger->debug('message goes here'); // saved data in var/log/system.log $this->_logger->info('message goes here'); } }
When your store goes live these log files are getting big in size day by day. If you have stored your custom module log in any of these log files then it will become very much difficult to check the log when something goes wrong.
The ideal way of logging custom module data is into custom log file. Today in this post we will see how to create log file in custom module in Magento 2.
To create a custom log file follow the below steps and add the below-mentioned files in your custom module.
Step 1: Define custom logger handler in module’s di.xml
file.
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <type name="Codextblog\Test\Logger\Handler"> <arguments> <argument name="filesystem" xsi:type="object">Magento\Framework\Filesystem\Driver\File</argument> </arguments> </type> <type name="Codextblog\Test\Logger\Logger"> <arguments> <argument name="name" xsi:type="string">customLogger</argument> <argument name="handlers" xsi:type="array"> <item name="system" xsi:type="object">Codextblog\Test\Logger\Handler</item> </argument> </arguments> </type> </config>
Step 2: Create Logger.php
class inside Logger directory.
<?php namespace Codextblog\Test\Logger; class Logger extends \Monolog\Logger { }
Step 3: Create Handler.php
class inside Logger directory.
<?php namespace Codextblog\Test\Logger; use Magento\Framework\Logger\Handler\Base; use Monolog\Logger; class Handler extends Base { protected $loggerType = Logger::INFO; protected $fileName = '/var/log/custom.log'; }
In this file, you can define the custom log file name. Here we have defined our custom log file name custom.log
Use Custom Log File in Custom Module in Magento 2
Now we have already created our custom logger class. We can use this class inside any of the PHP class and log the data into our custom log file.
<?php namespace Codextblog\Test\Block; class Test extends \Magento\Framework\View\Element\Template { protected $_customLogger; public function __construct( \Magento\Backend\Block\Template\Context $context, \Codextblog\Test\Logger\Logger $customLogger, array $data = [] ) { $this->_customLogger = $customLogger; parent::__construct($context, $data); } public function LogData() { //save data in var/log/custom.log $this->_customLogger->info('Message goes here'); //add array in log $this->_customLogger->info('API Params', $params); } }
Leave a Comment
(0 Comments)
Useful Magento 2 Articles
Author Info
Chirag
Connect With Me