In Magento 2, you can use the built-in logging system to add logs for debugging purposes. Here’s a step-by-step guide on how to add log in Magento2:
Initialize the Logger in your class
To start logging, you need to instantiate the \Psr\Log\LoggerInterface
in your class. You can do this by adding it to the constructor.
protected $logger;
public function __construct(
\Psr\Log\LoggerInterface $logger
) {
$this->logger = $logger;
parent::__construct();
}
Add Log Statements
Now, you can use the $this->logger
object to add log statements. There are different log levels available, such as debug
, info
, notice
, warning
, error
, critical
, and alert
. Choose the appropriate level based on the severity of the message.
$this->logger->debug('Debug message');
$this->logger->info('Info message');
$this->logger->error('Error message');
Viewing Logs
Magento 2 logs are typically stored in the var/log
directory. You can find logs for different areas and modules in this directory. View logs using tools like tail
or read the log files directly.
tail -f var/log/system.log
I hope this blog is useful to add log in Magento2. In case, I missed anything or need to add some more information, Don’t heisted to leave a comment in this blog, I’ll get back with some positive approach.
Keep loving ❤️ Keep inspiring 🤩 Keep liking 👍 No sharing 😄