How to Create a Custom Log File in Magento 2

Create a basic Magento 2 module

Create Registration.php

Directory

VendorName\ModuleName\registration.php

Content for this file

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
   'VendorName_ModuleName',
    __DIR__
);

Create module.xml

Directory

VendorName\ModuleName\etc\module.xml

Content for this file

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
   <module name="VendorName_ModuleName">
    </module>
</config>

Create the logger

Create Handler.php

Directory

VendorName\ModuleName\Logger\Handler.php

Content for this file

<?php
namespace VendorName\ModuleName\Logger;

use Magento\Framework\Logger\Handler\Base as BaseHandler;
use Monolog\Logger;

class Handler extends BaseHandler
{
    /**
     * Logging level
     * @var int
     */
    protected $loggerType = Logger::WARNING;

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

Define the logger

Directory

VendorName\ModuleName\etc\di.xml

Content for this file

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
    <type name="VendorName\ModuleName\Logger\Handler">
        <arguments>
            <argument name="filesystem" xsi:type="object">Magento\Framework\Filesystem\Driver\File</argument>
        </arguments>
    </type>
    <virtualType name="VendorName\ModuleName\Logger\Logger" type=”Monolog\Logger”>
        <arguments>
            <argument name="name" xsi:type="string">CustomLogger</argument>
            <argument name="handlers" xsi:type="array">
                <item name="system" xsi:type="object">Example\Customlog\Logger\Handler</item>
            </argument>
        </arguments>
    </virtualType>
    <type name=”VendorName\ModuleName\Controller\Index\Index”>
        <arguments>
            <argument name=”logger” xsi:type=”object”>Example\Customlog\Logger\Logger </argument>
        </arguments>
    </type>
</config>

Test the logger

Directory

VendorName\ModuleName\Controller\Index\Index.php

Content for this file

<?php
namespace VendorName\ModuleName\Controller\Index;

use \Psr\Log\LoggerInterface;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\Controller\Result\JsonFactory;
use Magento\Framework\View\Result\PageFactory;

class Index extends Action
{
    /**
     * @var PageFactory
     */
    protected $resultPageFactory;

    /**
     * @var JsonFactory
     */
    protected $resultJsonFactory;

    /**
     * @var LoggerInterface
     */
    protected $logger;

    public function __construct(
        Context $context,
        PageFactory $resultPageFactory,
        JsonFactory $resultJsonFactory,
        LoggerInterface $logger
    ) {
        $this->resultPageFactory = $resultPageFactory;
        $this->resultJsonFactory = $resultJsonFactory;
        $this->logger = $logger;
        parent::__construct($context);
    }

    public function execute()
    {
        // Our logger would not allow to use debug,info and notice log types
        $this->logger->debug('Log Debug');
        $this->logger->info('Log Info');
        $this->logger->notice('Log Notice');

        // We can use below log types via our custom log
        $this->logger->warning('Log Warning');
        $this->logger->error('Log Error');
        $this->logger->critical('Log Critical');
        $this->logger->alert('Log Alert');
        $this->logger->emergency('Log Emergency');

        $result = $this->resultJsonFactory->create();
       $data = ['message' => 'Welcome'];

        return $result->setData($data);
    }
}

 

If you are looking for Magento Developers, visit Magento Web Agency.