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.

Related Read:  How to Create a Group Product in Magento 2

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

Thats it. Enjoy Magento 2!!