O'Reilly logo

Magento 2 Developer's Guide by Branko Ajzele

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Logging

Magento supports the messages logging mechanism via its \Psr\Log\LoggerInterface class. The LoggerInterface class has a preference defined within app/etc/di.xml file for the Magento\Framework\Logger\Monolog class type. The actual crux of implementation is actually in the Monolog parent class named Monolog\Logger, which comes from the Monolog vendor.

The LoggerInterface class uses the following eight methods to write logs to the eight RFC 5424 levels:

  • debug
  • info
  • notice
  • warning
  • error
  • critical
  • alert
  • emergency

To use a logger, we need to pass the LoggerInterface class to a constructor of a class from within we want to use it and then simply make one of the following method calls:

$this->logger->log(\Monolog\Logger::DEBUG, 'debug msg'); $this->logger->log(\Monolog\Logger::INFO, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required