Webdesign Tutorials & Tips

How to create a custom log file in Magento 2

Your video will begin in 7
You can skip to video in 3

Thanks! Share it with your friends!

URL

You disliked this video. Thanks for the feedback!

Sorry, only registred users can create playlists.
URL

Description

Hi everybody, I am Cuong, Welcome back to my Magento 2 tutorial video series. #magento2tutorial #magento2 #magento2training #customlogfile

In past times, I was busy on a big project, so I didn't have the time for creating new videos and sharing it with everybody.

Today, I am going to show everybody the best practice, How to create a custom log file in Magento 2.

While you develop a Magento 2 website, it's very important to create the log text to monitor the errors and system information.

By default in Magento 2, you can use the class Psr\Log\LoggerInterface to create the log text, it will be saved in the system.log file. However, it is difficult to open or read this file when the file size is large because it saves all the log text of a Magento 2 website.

It is easier if you create a new custom log file and save the log text for each your task.

In this practice, I am going to create a new custom log file having the format customer-Y-m-d.log, with the "Y-m-d" will be the date of the log text saved. Every when the customer logs in I will save the customer information in this log file.

So what will I do in this practice?
1. We will create a new module named PHPCuong_CustomLogFile
2. We will define the new log file format customer-Y-m-d.log
3. We will hook to the event named customer_login saving the customer information in the customer-Y-m-d.log file.

Okie, let's go.

Let's complete this practice you need to follow steps by step:

Step 1: Declaring the new module called PHPCuong_CustomLogFile
- Create the namespace PHPCuong in the path app\code
- Create the module named CustomLogFile in the path app\code\PHPCuong
- Create the new file named registration.php in the path app\code\PHPCuong\CustomLogFile

- Create the new file named module.xml in the path app\code\PHPCuong\CustomLogFile\etc

End of step #1, I have been completed the codes to create the new module called PHPCuong_CustomLogFile.

Step 2: Define the new log file format customer-Y-m-d.log
- Create the new file named di.xml in the path app\code\PHPCuong\CustomLogFile\etc

- Create the new file named Customer.php in the path app\code\PHPCuong\CustomLogFile\Logger\Handler

- Create the new file named Customer.php in the path app\code\PHPCuong\CustomLogFile\Logger

Step 3: Hook to the event named customer_login saving the customer information in the customer-Y-m-d.log file
This event will be called when the customer logged in successed.

- Create the new file named events.xml in the path app\code\PHPCuong\CustomLogFile\etc\frontend

- Create the new file named CustomerLoginSuccess.php in the path app\code\PHPCuong\CustomLogFile\Observer

Step 4: Test and see the results
1. Run the following command lines:
php bin/magento setup:upgrade --keep-generated
php bin/magento cache:flush

2. Go to the storefront, the customer tries to log in

Let's complete this practice, you need to learn how to use an event and observer in Magento 2, you can learn it here http://bit.ly/2QwDfpL if you don't watch this video yet.

Thank you for watching guide. If you have any questions about this practice please feel free to leave a comment below.

Don't forget to like, comment, share and subscribe to my channel for getting the latest videos.

Please do not hesitate to contact me if you need me to join your Magento project. My rate is $25/hour in Magento 1 and $30/hour in Magento 2.

Get the source codes of this practice on the Github https://github.com/php-cuong/magento2-customlogfile

Post your comment

RSS