Log Print in Magento 2 is very important things for debug our code and it's very helpful for every developer.
Magento 2 provide log system in which we can track all the process like events, exception handling..etc
Magento 2 have inbuilt mechanism for manage logging in code based on Monolog library.
Magento 2 provide log functionality using below class
"Magento\Framework\Logger\Monolog"
This is main magento 2 log class and that is override in app/etc/di.xml. You can check from this location.
<preference for="Psr\Log\LoggerInterface" type="Magento\Framework\Logger\Monolog">
This LoggerInterface contain 9 different methods for handling Magento logging.
Please check list in below.
public function emergency($message, array $context = array());
=> This log saved in var/log/system.log file.
public function alert($message, array $context = array());
=> This log saved in var/log/system.log file.
public function critical($message, array $context = array());
=> This log saved in var/log/system.log file.
public function error($message, array $context = array());
=> This log saved in var/log/system.log file.
public function warning($message, array $context = array());
=> This log saved in var/log/system.log file.
public function notice($message, array $context = array());
=> This log saved in var/log/system.log file.
public function info($message, array $context = array());
=> This log saved in var/log/system.log file.
public function debug($message, array $context = array());
=> This log saved in var/log/debug.log file.
public function log($level, $message, array $context = array());
These are the 9 method we can use for logging or debugging magento 2 code.
For access these method in our custom module, just you need to create object of "Psr\Log\LoggerInterface" this interface in your class constructor and then using object call this method for logging.
Please check below example for how to print log ?
How to print log using logger class.
=> Using dependency injection we will create object of LoggerInterface and then use all these methods in our class for print log.
Please check below example.
class Testlog
{
private $logger;
public function __construct(\Psr\Log\LoggerInterface $logger) {
$this->logger = $logger;
}
public function testPrintLogMethod()
{
try {
this->logger->info("Success Message");
} catch (\Exception $e) {
$this->logger->critical($e->getMessage());
}
}
}
=> In above example, we have created object of logger and then use info and critical method for print log in var/log/system.log file.
=> so this is the way we can manage logging in magento 2.
How to create custom Log file in Magento 2?
If you want to create your custom log file during development then you can do this using below way.
This is different way and using zend log writer you can print log in separate file.
Please check below code.
$writer = new \Zend\Log\Writer\Stream(BP . '/var/log/testlogfile.log');
$logger = new \Zend\Log\Logger();
$logger->addWriter($writer);
$logger->info('Your text log message');
This code you can use just when you are developing task or debugging any issue. Please follow dependency injection for logging in magento 2.
That's it.