{"id":7348,"date":"2019-09-28T18:58:08","date_gmt":"2019-09-28T13:28:08","guid":{"rendered":"https:\/\/www.hiddentechies.com\/blog\/?p=7348"},"modified":"2019-10-04T11:23:29","modified_gmt":"2019-10-04T05:53:29","slug":"magento-2-write-log","status":"publish","type":"post","link":"https:\/\/www.hiddentechies.com\/blog\/magento-2\/magento-2-write-log\/","title":{"rendered":"Magento 2 &#8211; How to Write log"},"content":{"rendered":"<p>Logging is an important part of any development process. Logs assist you to spot an error and the reason for it. Magento 2 contains built-in logging solution based on Monolog Library.<\/p>\n<p>To start working with a logger, you must create an instance of \\Psr\\Log\\LoggerInterface. With this interface, you can call the following functions to write data to log files:<\/p>\n<p>alert()<br \/>\ncritical()<br \/>\ndebug()<br \/>\nemergency()<br \/>\nerror()<br \/>\ninfo()<br \/>\nlog()<br \/>\nnotice()<br \/>\nwarning()<\/p>\n<p>All log methods contains 2 arguments, first one is the message text and the second one is an optional array.<\/p>\n<pre class=\"lang:default decode:true \">$this-&gt;_logger-&gt; emergency($message);\r\n\/\/saved in var\/log\/system.log\r\n\r\n$this-&gt;_logger-&gt; alert($message);\r\n\/\/saved in var\/log\/system.log\r\n \r\n$this-&gt;_logger-&gt; critical($message);\r\n\/\/saved in var\/log\/system.log\r\n \r\n$this-&gt;_logger-&gt; error($message);\r\n\/\/saved in var\/log\/system.log\r\n \r\n$this-&gt;_logger-&gt; warning($message);\r\n\/\/saved in  var\/log\/system.log\r\n \r\n$this-&gt;_logger-&gt; notice($message);\r\n\/\/saved in var\/log\/system.log\r\n \r\n$this-&gt;_logger-&gt; info($message);\r\n\/\/saved in var\/log\/system.log\r\n \r\n$this-&gt;_logger-&gt; debug($message);\r\n\/\/saved in var\/log\/debug.log<\/pre>\n<p><strong>Note:<\/strong> The degug log is not generate when your Magento 2 website is in production mode.<\/p>\n<p>Below is an example of how to use logger in a custom method and how to log an exception.<\/p>\n<pre class=\"lang:default decode:true \">protected $_logger;\r\n    \r\npublic function __construct(\r\n\t\\Magento\\Backend\\Block\\Template\\Context $context,\r\n\t\\Psr\\Log\\LoggerInterface $logger,\r\n\tarray $data = []\r\n)\r\n{        \r\n\t$this-&gt;_logger = $logger;\r\n\tparent::__construct($context, $data);\r\n}\r\n\r\npublic function customMethod()\r\n{\r\n\t$this-&gt;_logger-&gt;info('Info Message');\r\n\t\r\n\t\/\/ Log as an Exception\r\n\ttry {\r\n\t\t\/\/do something\r\n\t} catch (\\Exception $e) {\r\n\t\t$this-&gt;_logger-&gt;critical('Something went wrong', ['exception' =&gt; $e]);\r\n\t}\r\n}<\/pre>\n<p>Thats it. Enjoy Magento 2!!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Logging is an important part of any development process. Logs assist you to spot an error and the reason for it. Magento 2 contains built-in logging solution based on Monolog Library. To start working with a logger, you must create an instance of \\Psr\\Log\\LoggerInterface. With this interface, you can call the following functions to write&#8230; <\/p>\n<div class=\"actions\"><a href=\"https:\/\/www.hiddentechies.com\/blog\/magento-2\/magento-2-write-log\/\">Continue Reading<\/a><\/div>\n","protected":false},"author":1,"featured_media":7377,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[59],"tags":[698,9,10,27,697],"_links":{"self":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/7348"}],"collection":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/comments?post=7348"}],"version-history":[{"count":1,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/7348\/revisions"}],"predecessor-version":[{"id":7349,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/7348\/revisions\/7349"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/media\/7377"}],"wp:attachment":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/media?parent=7348"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/categories?post=7348"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/tags?post=7348"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}