Skip to content
Code Assemble
  • Home
  • Magento 2
  • WordPress
  • Code Snippets
  • Privacy Policy
Code Assemble
  • Home
  • Magento 2
  • WordPress
  • Code Snippets
  • Privacy Policy

How to print logs in Magento 2?

  • November 29, 2020November 29, 2020
print logs in magento 2

In this post, We will share a code snippet and little hit explanation on how to print logs in custom and default logs file.

Generally, developers don’t write any code that logs meaningful and relevant messages for the feature they are writing. Writing code to log things may even be considered not essential and a waste of time by developers who work on small projects. This might be true to some point but the benefits surely outweigh the cost.

The very first thing you need to do is to make sure that the debug mode is enabled in the backend. Go to Stores -> Configuration -> Advanced -> Developer -> Debug and set “Yes” for logging in file.

Let’s get started 🙂

Print logs in the custom file (Temporary print log with a new file):
In magento2, You can write to the logs using the Zend a library like below :

$writer = new \Zend\Log\Writer\Stream(BP . '/var/log/logfile.log');
$logger = new \Zend\Log\Logger();
$logger->addWriter($writer);
$logger->info('Log Data'); // Log Data
$logger->info('Print Array Data'.print_r($myArrayVar, true)); // Print Array Data

Standard Magento 2 Logging (Factory Method):
By default, Magento uses Monolog. Monolog implemented as a preference for Psr\Log\LoggerInterface in the Magento application di.xml.

Monolog is a popular PHP logging solution with a wide range of handlers that enable you to build advanced logging strategies. Following is a summary of how Monolog works. You need to inject \Psr\Log\LoggerInterface class into the constructor to call logger object

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

public function logExample() {

    //To print string Output in debug.log
    $this->_logger->addDebug('Your Text Or Variables'); 

    // To print array Output in system.log
    $this->_logger->log('600', print_r($yourArray, true));

}

To print string Output in debug.log

You can place the below code to see the output in debug file.
\Magento\Framework\App\ObjectManager::getInstance()->get('Psr\Log\LoggerInterface')->debug('Your Message');

To print array Output in system.log

$myArray = array('test1'=>'123', 'test2'=>'123', 'test3'=>'123');
$level = '100'; // use one of: 100, 200, 250, 300, 400, 500, 550, 600
\Magento\Framework\App\ObjectManager::getInstance()
    ->get('Psr\Log\LoggerInterface')
    ->log($level, print_r($myArray, true));

In conclusion, it’s better to use the standard Magento log called Monolog. Monolog is a powerful logging tool that allows the user to use a wide range of handlers to their benefits, such as logging to files and Syslog, sending alerts and e-mails, logging database activity, etc.

Hope this helps! You can find more similar post on our site. 🙂

Tags:code assembleHow to print log in MAgento 2Magento2printing log in magento 2

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • WordPress asking for FTP credentials to install or update the plugin or theme
  • How to print logs in Magento 2?
  • How to create a custom table in Magento 2?
  • How to apply Schema Patch in Magento 2?

Categories

  • Code Snippets
  • Magento 2
  • WordPress
Copyright © 2020 Code Assemble. All Rights Reserved.