{"id":173,"date":"2018-09-05T12:32:33","date_gmt":"2018-09-05T12:32:33","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2018\/09\/05\/print-log-in-magento-2\/"},"modified":"2025-05-22T17:20:12","modified_gmt":"2025-05-22T11:50:12","slug":"print-log-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/print-log-in-magento-2\/","title":{"rendered":"How to Print Log in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Logs in Magento 2 consist of system information records for the analysis in the future. One of the most common examples of such events is the error log.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Developers know the pain of errors and the process to deliver a working solution. Debugging can be made easier for them by custom logs. It helps to spot an error and the reason for it, easily. Logs contribute to the visibility into Magento 2 system processes.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Now if you want to learn how to&nbsp;print log in Magento 2, you\u2019re at the right place! I have come up with 3 different methods to print log in Magento 2.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Using Helper Data<\/li>\n\n\n\n<li>Using Logger Interface<\/li>\n\n\n\n<li>Using Object Manager<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">Follow any of the below methods to create your own custom log files and get to solve the errors  <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Note:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Make sure that the debug mode is enabled in the backend. Go to&nbsp;<strong>Stores -&gt; Configuration -&gt; Advanced -&gt; Developer -&gt; Debug<\/strong>&nbsp;and set \u201c<strong>Yes<\/strong>\u201d to&nbsp;<strong>Log to File<\/strong>.<\/li>\n\n\n\n<li>Make sure your Magento store is in developer mode.<\/li>\n\n\n\n<li>For production mode, you have to run the below command in shell:<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>bin\/magento config:set dev\/debug\/debug_logging 1<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Methods to Print Log in Magento 2<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Method: 1&nbsp;Using Helper Data<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">create file&nbsp;[Vendor][Module]HelperData.php<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\nnamespace [Vendor]\\[Module]\\Helper;\nuse Zend\\Log\\Writer\\Stream;\nuse Zend\\Log\\Logger;\nclass Data extends AbstractHelper\n{\n\n    public function printLog($log)\n    {\n       $writer = new Stream(BP . '\/var\/log\/fileName.log');\n       $logger = new Logger();\n\n       $logger->addWriter($writer);\n       $logger->info($log);\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Simply call Helper class method where you want to print the log :<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">create file&nbsp;[Vendor]\\[Module]\\[Path]\\[fileName.php]<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\nnamespace [Vendor]\\[Module]\\[Path];\nuse [Vendor]\\[Module]\\Helper\\Data;\nprotected $helper;\nclass FileName\n{\n    public function __construct(Data $helperName)\n    {\n        $this->helper = $helperName;\n    }\n    $this->helper->printLog('msg for print');<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Method 2:&nbsp;&nbsp;Using Logger Interface<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">To enable the log in custom extension, go to&nbsp;app\\code\\[Vendor]\\[Module]\\[Path]\\[fileName.php]&nbsp;and add below code.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\nnamespace [Vendor]\\[Module]\\[Path];\nuse Psr\\Log\\LoggerInterface;\nclass FileName\n{\n    protected $logger;\n    public function __construct(LoggerInterface $logger;)\n    {\n        $this->logger = $logger;\n    }\n    $this->logger->debug('msg to print'); \/\/ print var\\log\\debug.log\n    $this->logger->info('msg to print');  \/\/ print var\\log\\system.log\n}<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Method 3:&nbsp;Using Object Manager<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">To&nbsp;print the message in&nbsp;var\\log\\system.log:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\\Magento\\Framework\\App\\ObjectManager::getInstance()-&gt;get('Psr\\Log\\LoggerInterface')-&gt;info('msg to print');<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Carry on your development process smoothly and if anytime you need to log variables or custom messages, you\u2019ll be having the easy method!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Happy Debugging!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Logs in Magento 2 consist of system information records for the analysis in the future. One of the most common examples of such events is&#8230;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-173","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/173","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=173"}],"version-history":[{"count":3,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/173\/revisions"}],"predecessor-version":[{"id":15607,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/173\/revisions\/15607"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=173"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=173"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=173"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}