How to Print Log in Magento 2

Logs in Magento 2 consist of system information records for the analysis in the future. One of the most common examples of such events is the error log.

Developers know the pain of errors and the process to deliver a working solution. Debugging can be made easier for them by custom logs. It helps to spot an error and the reason for it, easily. Logs contribute to the visibility into Magento 2 system processes.

Now if you want to learn how to print log in Magento 2, you’re at the right place! I have come up with 3 different methods to print log in Magento 2.

  1. Using Helper Data
  2. Using Logger Interface
  3. Using Object Manager

Follow any of the below methods to create your own custom log files and get to solve the errors 🙂

Note:

  1. Make sure that the debug mode is enabled in the backend. Go to Stores -> Configuration -> Advanced -> Developer -> Debug and set “Yes” to Log to File.
  2. Make sure your Magento store is in developer mode.
  3. For production mode, you have to run the below command in shell:

Methods to Print Log in Magento 2:

Method: 1 Using Helper Data

create file [Vendor]\[Module]\Helper\Data.php

Simply call Helper class method where you want to print the log :

create file [Vendor]\[Module]\[Path]\[fileName.php]

Method 2:  Using Logger Interface

To enable the log in custom extension, go to app\code\[Vendor]\[Module]\[Path]\[fileName.php] and add below code.

Method 3: Using ObjectManager

To print the message in var\log\system.log

Implement any of the above code to print log in Magento 2.

Carry on your development process smoothly and if anytime you need to log variables or custom messages, you’ll be having the easy method!

Please feel free to ask your doubts in the Comments section below. I’d be happy to solve them asap.
Rate the post with 5 stars if it helped you ease your code debugging ?

Happy Debugging!

Default image
Sagar Doshi
CEO / Managing Partner

An expert in his field, Sagar Doshi is the CEO and team leader in

meghrajtechnosoft.

His passion for Magento has inspired others in the team too. Apart from work, he is a cricket lover."

Articles: 26

Leave a Reply