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 Answer
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
}
}
-
I have to use this within the execute function within the observer? Commented Jul 15, 2019 at 5:21
-
can you provide me the controller file how to call this observer? 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. Commented Jul 15, 2019 at 5:48
-
-
1