Magento 2 logging

Today we will be talking about how to work with Magento 2 log. There is a different type of log you can log with Magento 2. For example debug log, information log, and other logs too. so let start with our log type.

A simple example of how to inject the logger class.

protected $logger;
public function __construct(\Psr\Log\LoggerInterface $logger)
{
    $this->logger = $logger;
}

Debug Log Method

$this->_logger->addDebug($message);

log location: var/log/system.log

Info Log Method

$this->_logger->addInfo($message);

log location: var/log/exception.log

Notice Log Method

$this->_logger->addNotice($message);

log location: var/log/exception.log

Error Log Method

$this->_logger->addError($message);

log location: var/log/exception.log

Critical or Exception Log Method

$this->_logger->critical($message);

log location: var/log/exception.log

Qaisar Satti

Hi, I'm Qaisar Satti! I've been a developer for over 20 years, and now I love sharing what I've learned through tutorials and guides. Whether you're working with Magento, PrestaShop, or WooCommerce, my goal is to make your development journey a bit easier and more fun. When I'm not coding or writing, you can find me exploring new tech trends and hanging out with the amazing developer community. Thanks for stopping by, and happy coding!

Leave a Reply