{"id":1647,"date":"2023-03-25T21:47:00","date_gmt":"2023-03-25T13:47:00","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1647"},"modified":"2023-04-23T21:57:35","modified_gmt":"2023-04-23T13:57:35","slug":"magento-2-logging","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/25\/magento-2-logging\/","title":{"rendered":"Magento 2\u65e5\u5fd7\u6253\u5370"},"content":{"rendered":"<pre><code class=\"language-php\">protected $logger;\npublic function __construct(\\Psr\\Log\\LoggerInterface $logger)\n{\n    $this-&gt;logger = $logger;\n}<\/code><\/pre>\n<p><!-- more --><\/p>\n<p>You use debug, exception, system for PSR Logger for example:<\/p>\n<pre><code class=\"language-php\">$this-&gt;logger-&gt;info($message);\n$this-&gt;logger-&gt;debug($message);<\/code><\/pre>\n<p>HINT:<\/p>\n<blockquote>\n<p>Don&#8217;t forget to run <code>php bin\/magento setup:di:compile<\/code><\/p>\n<\/blockquote>\n<p>Refer: <a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/magento.stackexchange.com\/questions\/92434\/magento-2-replacement-for-magelog-method\" title=\"magento-2-replacement-for-magelog-method\">magento-2-replacement-for-magelog-method<\/a><\/p>\n<pre><code class=\"language-php\">&lt;?php\n# Logging mechanisms in Magento 2 as Mage::Log is not globally\n# inherited anymore like in M1, and new vendor options exist.\n# Original sauce: https:\/\/magento.stackexchange.com\/a\/92442\/69 \n\n# Mage::Log in M2\n# You can also write to the logs using the Zend library:\n$writer = new \\Zend\\Log\\Writer\\Stream(BP . &#039;\/var\/log\/test.log&#039;);\n$logger = new \\Zend\\Log\\Logger();\n$logger-&gt;addWriter($writer);\n$logger-&gt;info(&#039;Your text message&#039;);\nEdited\n\n# You can also print PHP objects and arrays:\n$logger-&gt;info(print_r($yourArray, true));\n\n# OR Mono\/PSR Logger which is new in M2\nprotected $logger;\npublic function __construct(\\Psr\\Log\\LoggerInterface $logger)\n{\n    $this-&gt;logger = $logger;\n}\n\n# You use debug, exception, system for psr logger for example\n$this-&gt;logger-&gt;info($message);\n$this-&gt;logger-&gt;debug($message);<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>protected $logger; public function __construct(\\Psr\\Log [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[78],"tags":[],"class_list":["post-1647","post","type-post","status-publish","format-standard","hentry","category-magento"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1647","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1647"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1647\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1647"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1647"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1647"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}