How to create custom log file in Magento 2 module?

You can create your custom log file as a module in the var/log folder. You can generate a .log file based on your module name or functionality-related name.

You need to create an etc/di.xml file and specify the virtual type for the logger.

Create a Virtual type for the class Magento\Framework\Logger\Handler\Base to specify your custom file name.

Custom Log file name: sms-message.log 

Above file will be generated automatically at var/log folder.

<virtualType name="SmsMessageDebugLog" type="Magento\Framework\Logger\Handler\Base">
   <arguments>
       <argument name="fileName" xsi:type="string">/var/log/sms-message.log</argument>
   </arguments>
</virtualType>

Here name attribute value(SmsMessageDebugLog) must be unique in a system.

Create Handler Virtual type for class Magento\Framework\Logger\Monolog and the name value (SmsCustomLogger) must be unique in a system.

<virtualType name="SmsCustomLogger" type="Magento\Framework\Logger\Monolog">
    <arguments>
        <argument name="handlers" xsi:type="array">
        <item name="debug" xsi:type="object">SmsMessageDebugLog</item>
        </argument>
    </arguments>
</virtualType>

Above, The virtual class SmsMessageDebugLog is injected into the handler of the $logger property in the Rbj\Sms\Model\SmsApi class.

Let’s say you need to write a log from the model file,

Model File: Rbj\Sms\Model\SmsApi

<type name="Rbj\Sms\Model\SmsApi">
    <arguments>
        <argument name="logger" xsi:type="object">SmsCustomLogger</argument>
    </arguments>
</type>

In the type property name value must be your PHP Class where you want to log your module customization.

The final code should be in etc/di.xml looks file,

<virtualType name="SmsMessageDebugLog" type="Magento\Framework\Logger\Handler\Base">
   <arguments>
       <argument name="fileName" xsi:type="string">/var/log/sms-message.log</argument>
   </arguments>
</virtualType>
<virtualType name="SmsCustomLogger" type="Magento\Framework\Logger\Monolog">
    <arguments>
        <argument name="handlers" xsi:type="array">
        <item name="debug" xsi:type="object">SmsMessageDebugLog</item>
        </argument>
    </arguments>
</virtualType>

<type name="Rbj\Sms\Model\SmsApi">
    <arguments>
        <argument name="logger" xsi:type="object">SmsCustomLogger</argument>
    </arguments>
</type>

Now in the custom Model class, You can debug the log with the help of $logger and use any of the available logger functions like, error, alert, info, critical, emergency, notice, debug and warning.

<?php
declare(strict_types=1);

namespace Rbj\Sms\Model;

use Psr\Log\LoggerInterface;

/**
 * Send SMS API Implementation Model.
 */
class SmsApi
{
    public function __construct(
        private readonly LoggerInterface $logger
    ){
    }

    public function sendSms()
    {
        try {
            // code to send sms via API.
        } catch (\Exception $e) {
            $this->logger->error('Error on send text message' . $e->getMessage());
        }
    }
}

Now you can able to see log inside, var/log/sms-message.log file.