Adobe Commerce 2.3 reached end of support in September 2022.

Example - logging to a custom log file

The Magento\Framework\Logger module contains the following handler classes:

Class Log file
Magento\Framework\Logger\Handler\Base -
Magento\Framework\Logger\Handler\Debug /var/log/debug.log
Magento\Framework\Logger\Handler\Exception /var/log/exception.log
Magento\Framework\Logger\Handler\Syslog -
Magento\Framework\Logger\Handler\System /var/log/system.log

You may find them in the lib/internal/Magento/Framework/Logger/Handler directory.

You can use one of the following approaches for logging into a custom file:

  • Set up a custom log file in the di.xml
  • Set up a custom file in the custom logger handler class

Set up a custom log file in the di.xml

This example shows how to use virtual types to log debug messages into a custom log file instead of a standard Magento /var/log/debug.log.

  1. In the di.xml file of your module, define a custom log file as a virtual type.

    1
    2
    3
    4
    5
    
    <virtualType name="Magento\Payment\Model\Method\MyCustomDebug" type="Magento\Framework\Logger\Handler\Base">
        <arguments>
            <argument name="fileName" xsi:type="string">/var/log/payment.log</argument>
         </arguments>
    </virtualType>
    

    The name value of Magento\Payment\Model\Method\MyCustomDebug must be unique.

  2. Define the handler in another virtual type with a unique name:

    1
    2
    3
    4
    5
    6
    7
    
    <virtualType name="Magento\Payment\Model\Method\MyCustomLogger" type="Magento\Framework\Logger\Monolog">
        <arguments>
            <argument name="handlers" xsi:type="array">
                <item name="debug" xsi:type="object">Magento\Payment\Model\Method\MyCustomDebug</item>
            </argument>
        </arguments>
    </virtualType>
    
  3. Inject the MyCustomLogger virtual type in the Magento\Payment\Model\Method\Logger object:

    1
    2
    3
    4
    5
    
    <type name="Magento\Payment\Model\Method\Logger">
        <arguments>
            <argument name="logger" xsi:type="object">Magento\Payment\Model\Method\MyCustomLogger</argument>
        </arguments>
    </type>
    
  4. The virtual class Magento\Payment\Model\Method\MyCustomDebug will be injected into the debug handler of the $logger property in the Magento\Payment\Model\Method\Logger class.

    1
    2
    3
    4
    
    ...
    <argument name="handlers" xsi:type="array">
        <item name="debug" xsi:type="object">Magento\Payment\Model\Method\MyCustomDebug</item>
    </argument>
    

Exception messages are logged into the /var/log/payment.log file.

Set up a custom file in the custom logger handler class

This example shows how to use a custom logger handler class to log error messages into a specific log file.

  1. Create a class that logs data. In this example, the class is defined in app/code/Vendor/ModuleName/Logger/Handler/ErrorHandler.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
    
    <?php
    /**
     * @author Vendor
     * @copyright Copyright (c) 2019 Vendor (https://www.vendor.com/)
     */
    namespace Vendor\ModuleName\Logger\Handler;
    
    use Magento\Framework\Logger\Handler\Base as BaseHandler;
    use Monolog\Logger as MonologLogger;
    
    /**
     * Class ErrorHandler
     */
    class ErrorHandler extends BaseHandler
    {
        /**
         * Logging level
         *
         * @var int
         */
        protected $loggerType = MonologLogger::ERROR;
    
        /**
         * File name
         *
         * @var string
         */
        protected $fileName = '/var/log/my_custom_logger/error.log';
    }
    
  2. Define the handler for this class as a virtual type in the module’s di.xml file.

    1
    2
    3
    4
    5
    6
    7
    
    <virtualType name="MyCustomLogger" type="Magento\Framework\Logger\Monolog">
        <arguments>
            <argument name="handlers" xsi:type="array">
                <item name="error" xsi:type="object">Vendor\ModuleName\Logger\Handler\ErrorHandler</item>
            </argument>
        </arguments>
    </virtualType>
    

    MyCustomLogger is a unique identifier.

  3. In the type definition, specify the class name where the custom logger handler will be injected. Use the virtual type name from the previous step as an argument for this type.

    1
    2
    3
    4
    5
    
    <type name="Vendor\ModuleName\Observer\MyObserver">
        <arguments>
            <argument name="logger" xsi:type="object">MyCustomLogger</argument>
        </arguments>
    </type>
    

    Source code of Vendor\ModuleName\Observer\MyObserver class:

    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
    42
    43
    44
    45
    46
    47
    
    <?php
    /**
     * @author Vendor
     * @copyright Copyright (c) 2019 Vendor (https://www.vendor.com/)
     */
    declare(strict_types=1);
    
    namespace Vendor\ModuleName\Observer;
    
    use Psr\Log\LoggerInterface as PsrLoggerInterface;
    use Exception;
    use Magento\Framework\Event\ObserverInterface;
    use Magento\Framework\Event\Observer;
    
    /**
     * Class MyObserver
     */
    class MyObserver implements ObserverInterface
    {
        /**
         * @var PsrLoggerInterface
         */
        private $logger;
    
        /**
         * MyObserver constructor.
         *
         * @param PsrLoggerInterface $logger
         */
        public function __construct(
            PsrLoggerInterface $logger
        ) {
            $this->logger = $logger;
        }
    
        /**
         * @param Observer $observer
         */
        public function execute(Observer $observer)
        {
            try {
                // some code goes here
            } catch (Exception $e) {
                $this->logger->error($e->getMessage());
            }
        }
    }
    
  4. The class Vendor\ModuleName\Logger\Handler\ErrorHandler will be injected into the error handler of the $logger property in the Vendor\ModuleName\Observer\MyObserver.

    1
    2
    3
    4
    5
    
    ...
    <argument name="handlers" xsi:type="array">
        <item name="error" xsi:type="object">Vendor\ModuleName\Logger\Handler\ErrorHandler</item>
    </argument>
    ...
    

Exception messages will be logged into /var/log/my_custom_logger/error.log file.