Read More
Read More
Menu Close

    Magento2 Custom Log File Using Monolog

    Magento2 Custom Log File Using Monolog – If having issue with creating your own log file then solution is here,here you can learn how to create your own log file.
    Here I am going to create a “customfile.log” file using Default Magento2 logger(Monolog), so now I am going to explain step by step.

    Step 1 : Declaration of chanel and handlers
    create file di.xml by following the path your_magento_root/app/code/Webkul/Modulename/etc/di.xml

    <?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="Webkul\Modulename\Logger\Handler">
            <arguments>
                <argument name="filesystem" xsi:type="object">Magento\Framework\Filesystem\Driver\File</argument>
            </arguments>
        </type>
        <type name="Webkul\Modulename\Logger\Logger">
            <arguments>
                <argument name="name" xsi:type="string">customLogHandler</argument>
                <argument name="handlers"  xsi:type="array">
                    <item name="system" xsi:type="object">Webkul\Modulename\Logger\Handler</item>
                </argument>
            </arguments>
        </type>
    </config>
    

    Step 2 : Creation of Webkul\Modulename\Logger\Handler

    Here I am defining the custom log file name

    namespace Webkul\Modulename\Logger;
    <?php
    class Handler extends \Magento\Framework\Logger\Handler\Base
    {
        /**
         * Logging level
         * @var int
         */
        protected $loggerType = Logger::INFO;
    
        /**
         * File name
         * @var string
         */
        protected $fileName = '/var/log/customfile.log';
    }

    Step 3 : Creation of Webkul\Modulename\Logger\Logger

    Searching for an experienced
    Magento 2 Company ?
    Read More
    namespace Webkul\Modulename\Logger;
    <?php
    class Logger extends \Monolog\Logger
    {
    }
    

    Step 4 : log data in your log file “customfile.log”

    <?php
    namespace Webkul\Modulename\Controller\Index;
    
    use Magento\Framework\App\Action\Action;
    use Magento\Framework\App\Action\Context;
    
    class Index extends Action
    {
        /**
         * Logging instance
         * @var \Webkul\Modulename\Logger\Logger
         */
        protected $_logger;
    
        /**
         * @param Context $context
         * @param \Webkul\Modulename\Logger\Logger $logger
         */
        public function __construct(
            Context $context,
            \Webkul\Modulename\Logger\Logger $logger
        ) {
            $this->_logger = $logger;
            parent::__construct($context);
        }
    
    
        /**
         * Default customer account page
         *
         * @return \Magento\Framework\View\Result\Page
         */
        public function execute()
        {
            $data = $this->getRequest()->getParams();
            $this->_logger->info(print_r($data)); // log array Data to customfile.log
            $this->_logger->info("Some text string data"); // log string Data to customfile.log
            //  Write Your Code Here
            return $this->resultRedirectFactory->create()->setPath('*/*/*');
        }
    }

    So in this way you can create your own log file. 🙂

    . . .
    Discuss on Helpdesk

    Leave a Comment

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


    5 comments

  • Prabhat kumar jena
    Worked like a charm. Github code helped. Thanks for the article. Looking forward to a more article that teaches us such valuable content
  • Kirill from Magecom
    Hi. In my case (magento 2.3) when i’m used given example, i found an error in this piece of code: . The endpoint of type must be Logger, not Handler. With this correction i’m have fully working module.
  • Vaibhav Ahalpara
    It’s not working for me too error was missing argument $name
  • Rajkumar
    Hello, WebKUl

    Your code not working for me? ,i follow your steps, based on my project , customfile.log not cretaed in my var/log folder.

    • webkul
      Hello,
      That’s very strange it should work because I tested it again and it’s working, may be there is something missing that’s why it’s not working at your end so please check the complete mod https://github.com/webkul/magento2_customlog/ I hope this will help you out.
      Thanks!
  • Back to Top