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, 'info msg'); $this->logger->log(\Monolog\Logger::NOTICE, 'notice msg'); $this->logger->log(\Monolog\Logger::WARNING, 'warning msg'); $this->logger->log(\Monolog\Logger::ERROR, 'error msg'); ...