Magento 2 – How to Write log

Logging is an important part of any development process. Logs assist you to spot an error and the reason for it. Magento 2 contains built-in logging solution based on Monolog Library.

To start working with a logger, you must create an instance of \Psr\Log\LoggerInterface. With this interface, you can call the following functions to write data to log files:

alert()
critical()
debug()
emergency()
error()
info()
log()
notice()
warning()

All log methods contains 2 arguments, first one is the message text and the second one is an optional array.

Note: The degug log is not generate when your Magento 2 website is in production mode.

Below is an example of how to use logger in a custom method and how to log an exception.

Thats it. Enjoy Magento 2!!

1 Comment

  1. Interesting information I got from here… Thanks, team!

Leave a Reply

Your email address will not be published. Required fields are marked *