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.
Asked
Active
Viewed 174 times
1
-
I need to print in customer log and not just fetch details – Naiwrita09 Jul 15 '19 at 05:48
1 Answers
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
}
}
anonymous
- 3,722
- 3
- 25
- 67
-
I have to use this within the execute function within the observer? – Naiwrita09 Jul 15 '19 at 05:21
-
-
No need for controller You only need to create observer and print data to log using above code. – anonymous Jul 15 '19 at 05:48
-
-
public function execute(\Magento\Framework\Event\Observer $observer) {
$writer = new \Zend\Log\Writer\Stream(BP . '/var/log/customerdata.log'); $logger = new \Zend\Log\Logger(); $logger->addWriter($writer);
– Naiwrita09 Jul 15 '19 at 05:51$customer = $observer->getEvent()->getCustomer(); echo $customer->getName(); //Get customer name $logger->info(print_r($customer->getEmail(),true)); die(); } -
-
Give permission to var folder it will. Leave log for now, Print just customer email and exit code if you can see email printed then debug for log ahead. – anonymous Jul 15 '19 at 05:52
-
normal printing of the name is happening but no log is generated, based on the answers given in the above link you commented – Naiwrita09 Jul 15 '19 at 06:18
-
Try this Its similar but little bit different https://magento.stackexchange.com/a/92442/49826 – anonymous Jul 15 '19 at 06:19
-
-
-
-
Check I've updated whole code Its working for me, New log file geneeated and also prints my name :) Just to inform you have you run upgrade after creating xml and observer file Its necessary if not please do that and also apply permission to var folder to create new file. – anonymous Jul 15 '19 at 06:34
-
-
1