0

I want to create a new log file which will print the admin details who deletes a customer profile from admin panel in magento 2. How can I do that?

2

In your di.xml

  1. app/code/Vendor/Module/etc/di.xml
<type name="Vendor\Module\Logger\Handler">
        <arguments>
            <argument name="filesystem" xsi:type="object">Magento\Framework\Filesystem\Driver\File</argument>
        </arguments>
    </type>
    <type name="Vendor\Module\Logger\Logger">
        <arguments>
            <argument name="name" xsi:type="string">customLogger</argument>
            <argument name="handlers"  xsi:type="array">
                <item name="system" xsi:type="object">Vendor\Module\Logger\Handler</item>
            </argument>
        </arguments>
    </type>
  1. Create a Logger folder in your module and Logger.php file

app/code/Vendor/Module/Logger/Logger.php

<?php

namespace Vendor\Module\Logger;

class Logger extends \Monolog\Logger
{
}
  1. Create a Handler.php File in Logger Folder

app/code/Vendor/Module/Logger/Handler.php

<?php
namespace Vendor\Module\Logger;

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

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

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

Use this in your Module instead of this use Psr\Log\LoggerInterface; add your custom logger use Vendor\Module\Logger\Logger;

use Vendor\Module\Logger\Logger;

class Test
{
  public function __construct(
        Logger $logger
    ) {
        $this->logger = $logger;
    }
}

You can use it as

$this->logger->info('this is custom logger);

Hope it helps.

  • In this code how can I implement the plugin code. so that it prints the details and what should be included in the logger class – Naiwrita09 Jul 13 '19 at 9:46
  • I have updated an answer please check. – Mohit Rane Jul 13 '19 at 9:51
  • sorry but I didnt get the changes. this code wont print the admin details and how to connect the plugin with this – Naiwrita09 Jul 13 '19 at 9:53
  • In your plugin instead of this use Psr\Log\LoggerInterface; add your custom logger use Vendor\Module\Logger\Logger; – Mohit Rane Jul 13 '19 at 9:55
  • You can mark it as accepted answer if it's solved your issue so it could help others. – Mohit Rane Jul 13 '19 at 10:03

Not the answer you're looking for? Browse other questions tagged or ask your own question.