Reading list Switch to dark mode

    Magento2 Custom Log File Using Monolog

    Updated 4 March 2024

    Magento 2 Custom Log File Using Monolog – Creating a custom module in magento 2 often requires to identify the errors and information logs for various reasons such as debugging .

    But it is difficult to find the relevant information that is required in one single log file. Therefore, we should always create a custom log file for every module in Magento.

    If you are having issue with creating your own custom log file then you can find the solution here in this blog,here you can learn how to create your own custom log file.

    Here I am going to create a custom log file named “customfile.log” file using Default Magento 2 logger (Monolog), so now I am going to explain step by step please follow the steps below to create one of your own.

    Step 1 : Declaration of Logger and handlers in Magento 2 custom module

    Searching for an experienced
    Magento 2 Company ?
    Find out More

    Create a file di.xml by following the path your_magento_root/app/code/Webkul/Modulename/etc/di.xml

    <?xml version="1.0"?>
    <config xmlns:xsi="" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
        <type name="Webkul\Modulename\Logger\Handler">
                <argument name="filesystem" xsi:type="object">Magento\Framework\Filesystem\Driver\File</argument>
        <type name="Webkul\Modulename\Logger\Logger">
                <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>

    Step 2 : Create a file Handler.php at Webkul\Modulename\Logger\ which will extend \Magento\Framework\Logger\Handler\Base class

    Here I am defining the custom log file name as “customfile.log” in Handler class

    namespace Webkul\Modulename\Logger;
    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 : Create a file Logger.php at following path Webkul\Modulename\Logger\ which will extend the Monolog\Logger class

    namespace Webkul\Modulename\Logger;
    class Logger extends \Monolog\Logger

    Step 4 : log any test data in your log file “customfile.log” by calling your custom Magento 2 Controller or Helper method, Here I am using Controller to log data in my custom log file

    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;
         * 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('*/*/*');

    As you can see we have to call the logger methods to create a log entry in the custom log file.

    Here I have used info() method.

    Other methods that you can use are :



    So in this way you can create your own log file. ­čÖé

    You can also check the below links :

    . . .

    Leave a Comment

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


  • Prabhat kumar jena
  • Kirill from Magecom
  • Vaibhav Ahalpara
  • Rajkumar
    • webkul
  • Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home