1

I have to print the login details of the customer and when he logged in, into the log file. How to implement the following problem in Magento 2.

1
  • I need to print in customer log and not just fetch details
    – Naiwrita09
    Commented Jul 15, 2019 at 5:48

1 Answer 1

1

You can use event customer_login which will give you all customer data. Check here.

https://magento.stackexchange.com/a/178884/49826

For print in logs do as below.

Vendor/Module/etc/events.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="customer_login">
        <observer name="customer_login_observer" instance="LazyCoder\PaymentDeadlines\Observer\CustomerLogin" />
    </event>
</config>

Vendor/Module/Obsever/CustomerLogin.php

    <?php
namespace LazyCoder\PaymentDeadlines\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Event\Observer as EventObserver;

class CustomerLogin implements ObserverInterface
{
    public function execute(EventObserver $observer)
    {
        $customer = $observer->getEvent()->getCustomer();
        $name =  $customer->getName(); //Get customer name


        $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/logfile.log');
        $logger = new \Zend\Log\Logger();
        $logger->addWriter($writer);
        $logger->info($name); // Simple Text Log


    }
}
15
  • I have to use this within the execute function within the observer?
    – Naiwrita09
    Commented Jul 15, 2019 at 5:21
  • can you provide me the controller file how to call this observer?
    – Naiwrita09
    Commented Jul 15, 2019 at 5:33
  • No need for controller You only need to create observer and print data to log using above code.
    – anonymous
    Commented Jul 15, 2019 at 5:48
  • My observer file:
    – Naiwrita09
    Commented Jul 15, 2019 at 5:50
  • 1
    yes It worked thank you so much
    – Naiwrita09
    Commented Jul 15, 2019 at 6:58

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