Magento 2: Create a Custom Log File

Magento 2: Create a Custom Log File - SbDevBlog

Logging is essential for the development and execution processes. Magento 2 itself logs the different content on the respective log file. For example, Magento 2 logs any runtime exception into the exception.log.

These errors can be of several types, like info, debug, critical, or error. System.log, exception.log, debug.log, and cron.log are default files generated by Magento 2. However, we can also create a custom log file with a suitable name.

Creating a custom log file is very easy. We must create a custom handler specifying fileSystem as an argument of the below object type at the type tag.

\Magento\Framework\Filesystem\Driver\File

Now, we must inject our custom handler into our custom logger as an argument in the type tag in the di.xml file. So, let’s create a custom logger.

Magento 2: Create a Custom Log File

Create a custom handler and inject it into a custom logger using a 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="SbDevBlog\Logger\Logger\Handler">
        <arguments>
            <argument name="filesystem" xsi:type="object">Magento\Framework\Filesystem\Driver\File</argument>
        </arguments>
    </type>
    <type name="SbDevBlog\Logger\Logger\Logger">
        <arguments>
            <argument name="name" xsi:type="string">sbdevbloglogger</argument>
            <argument name="handlers"  xsi:type="array">
                <item name="system" xsi:type="object">SbDevBlog\Logger\Logger\Handler</item>
            </argument>
        </arguments>
    </type>
</config>

We must create a handler and logger file defined in di.xml. The file name can be set using the handler.

The handler class must extend the base handler class.

\Magento\Framework\Logger\Handler\Base

<?php
namespace SbDevBlog\Logger\Logger;

use Monolog\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/sbdevblog.log';
}

A logger file. Logger class must extend \Monolog\Logger class.

<?php
namespace SbDevBlog\Logger\Logger;

class Logger extends \Monolog\Logger
{
}

That’s it. We must inject the logger class we have created where we need to log information like errors.

<?php
namespace SbDevBlog\Logger\Services;

class CheckLogger
{
    /**
     * Logging instance
     * @var \SbDevBlog\Logger\Logger\Logger
     */
    protected $logger;

    /**
     * Constructor
     * @param \SbDevBlog\Logger\Logger\Logger $logger
     */
    public function __construct(
        \SbDevBlog\Logger\Logger\Logger $logger
    ) {
        $this->logger = $logger;
    }

    public function loggingInfo()
    {
        $this->logger->info('This is test log from custome logger');
    }
}

Thanks for reading the post- Magento 2: Create a Custom Log File. Please subscribe to the SbDevBlog and share it with your connections. Please use the comment box for your feedback.

Click here to upload files programmatically.

Checkout GIT Repo

Note: Please verify the code of this blog and the relevant git repository before using it in production.
sb dev blog adobe commece Magento 2

🙂 HAPPY CODING 🙂

Leave a Reply

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