today I will talk about how you can create a custom log file for your Magento 2 custom module.

In Magento 1 for example to create a custom log file you can call Mage class and the static function log like this “Mage::log(‘mymessage’,null,’mylogfile.log’)”.

In Magento 2 you have to use a Dependecy Injection to be possible create a custom log file. As the Magento 2 documentation says “Magento 2 uses dependency injection as an alternative to the Magento 1.x Mage class. Dependency injection is when an object’s dependencies are provided to it by its external environment instead of manually creating them internally.” .

First in you Magento module you have to create a di.xml file the etc folder of your custom module. For example my custom module Package is called Correios and my namespace is Igorludgero. So the path to the file is app/code/Igorludgero/Correios/etc/di.xml.

In the di.xml you will put this code:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    <type name="Igorludgero\Correios\Logger\Handler">
            <argument name="filesystem" xsi:type="object">Magento\Framework\Filesystem\Driver\File</argument>
    <type name="Igorludgero\Correios\Logger\Logger">
            <argument name="name" xsi:type="string">ilcorreioslog</argument>
            <argument name="handlers"  xsi:type="array">
                <item name="system" xsi:type="object">Igorludgero\Correios\Logger\Handler</item>

Look we have two “type” nodes, one for the Handler class and the another to the Logger class.

In the Handler node you have to inform you will create a filesystem, in Magento framework the class to file systems is Magento\Framework\Filesystem\Driver\File.

In the second “type” node you will inform in “argument name” the name of your custom log file. In the type name you will write the path of your Logger class, in my case Igorludgero\Correios\Logger\Logger. In the argumen “handlers” you will provide the class of your Handler defined in the previous type node, in my case Igorludgero\Correios\Logger\Handler.

After this you have to create the two files that you defined in di.xml, the Handler and Logger class. In your custom module directory create a Logger folder and inside it create the two php classes.

This is my Handler class:

namespace Igorludgero\Correios\Logger;

class Handler extends \Magento\Framework\Logger\Handler\Base
     * Logging level
     * @var int
    protected $loggerType = \Monolog\Logger::INFO;

     * File name
     * @var string
    protected $fileName = '/var/log/ilcorreios.log';

In $loggerType you will declare if your log is an information, an error or other error type, and in $fileName the path of your custom log.

And in your Logger class you have only to declare the class. This is my Logger class:

namespace Igorludgero\Correios\Logger;

class Logger extends \Monolog\Logger

To use your custom log you have to provide your Logger class in the construct method of your block or other class. For example:

public function __construct(\Igorludgero\Correios\Logger\Logger $logger)
    $this->_logger = $logger;

And to log a message in your custom log file you can call the info or other method depending of the type of your log. In my case my log is a info log, so I call call the info method:


So I expect I helped about how to create a custom log file in Magento custom module. Thank’s for visit my website and see you in the next post =).

Useful links:


Magento 2 – How to create a custom log file

Igor Ludgero

PHP & Magento Developer from Brazil.

Post navigation

Leave a Reply

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

Pin It on Pinterest