{"id":8,"date":"2019-05-31T10:03:59","date_gmt":"2019-05-31T10:03:59","guid":{"rendered":"https:\/\/www.magetrend.com\/blog\/?p=8"},"modified":"2022-05-29T08:23:01","modified_gmt":"2022-05-29T08:23:01","slug":"magento-2-write-to-log","status":"publish","type":"post","link":"https:\/\/www.magetrend.com\/blog\/magento-2-write-to-log\/","title":{"rendered":"How write to log in Magento 2?"},"content":{"rendered":"\n<p>The following code snippet is the alternative to <strong>Mage::log(&#8216;*&#8217;)<\/strong> logging method in Magento 1. It&#8217;s a good way to log the errors while you are debugging or in the other situations when you need to write it temporary. It saves your time. The code snippet is in one piece, so you can simply paste it almost everywhere in Magento 2 code. <\/p>\n\n\n<div class=\"simple-container\"> <a class=\"simple-img\" href=\"https:\/\/www.magetrend.com\/magento-2-pdf-invoice?utm_source=article&utm_medium=post_8&utm_campaign=pos1\"><img decoding=\"async\"  src=\"\/blog\/wp-content\/uploads\/prod\/magento-2-pdf-templates.png\" \/><\/a> <div class=\"simple-row simple-title\"><a href=\"https:\/\/www.magetrend.com\/magento-2-pdf-invoice?utm_source=article&utm_medium=post_8&utm_campaign=pos1\">The Most Popular PDF Designer for Magento 2<\/a><\/div> <div class=\"simple-row simple-text\"><a href=\"https:\/\/www.magetrend.com\/magento-2-pdf-invoice?utm_source=article&utm_medium=post_8&utm_campaign=pos1\">with user-friendly PDF editor to build your own PDF look<\/a><\/div> <div class=\"simple-action\"><a href=\"https:\/\/www.magetrend.com\/magento-2-pdf-invoice?utm_source=article&utm_medium=post_8&utm_campaign=pos1\">Read More<\/a> <\/div> <\/div>\n\n\n\n<pre class=\"wp-block-code\"><code>\\Magento\\Framework\\App\\ObjectManager::getInstance()\n    -&gt;get(\\Psr\\Log\\LoggerInterface::class)\n    -&gt;debug('<span class=\"highlight\">message<\/span>');<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">The right way to write to log<\/h2>\n\n\n\n<p>But there is a better way to write a log. According to Magento recommendations the better way is to include the logger in a class constructor and call it directly (dependency injection method). Bellow is the example of code how to use it properly.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public $logger;\n\npublic function __construct(\\Psr\\Log\\LoggerInterface $logger)\n{\n    $this-&gt;logger = $logger;\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>$this-&gt;logger-&gt;debug('<span class=\"highlight\">message<\/span>');\n$this-&gt;logger-&gt;error('<span class=\"highlight\">message<\/span>');<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">8 different log record types<\/h2>\n\n\n\n<p>The Psr\\Log has different record types called &#8220;log levels&#8221;. They let us to group and handle the logs by a purpose. Here is the list of all available types in Magento 2 and how to use them:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/System is unusable.\n$this-&gt;logger-&gt;<span class=\"highlight\">emergency<\/span>('Something goes wrong');<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/Action must be taken immediately. This should trigger the SMS alerts and wake you up.\n$this-&gt;logger-&gt;<span class=\"highlight\">alert<\/span>('Entire website down');<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/Critical conditions\n$this-&gt;logger-&gt;<span class=\"highlight\">critical<\/span>('Application component unavailable');<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/Runtime errors that do not require immediate action but should typically be logged and monitored.\n$this-&gt;logger-&gt;<span class=\"highlight\">error<\/span>('Unable to send email');<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code> \/\/Exceptional occurrences that are not errors.\n $this-&gt;logger-&gt;<span class=\"highlight\">warning<\/span>('Use of deprecated APIs');<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/Normal but significant events.\n$this-&gt;logger-&gt;<span class=\"highlight\">notice<\/span>('Undefined index of name');<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/Interesting events.\n$this-&gt;logger-&gt;<span class=\"highlight\">info<\/span>('User logs in');<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/Detailed debug information. Working only in default and developer magento modes.\n $this-&gt;logger-&gt;<span class=\"highlight\">debug<\/span>('Working here.');<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Where my logs are stored?<\/h2>\n\n\n\n<p>The directory of log files is always the same <strong>MAGE_DIR\/var\/log\/<\/strong>. According to log record type, your log can be written to one of the files bellow. Make sure Magento has permissions to write a file otherwise log files will be without your records. There are few more situations when records can be missed to write to log file, but about that in our next paragraph.<\/p>\n\n\n\n<p>All logs except debug will be saved to: <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var\/log\/system.log<\/code><\/pre>\n\n\n\n<p>and only debugging logs will be saved to:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var\/log\/debug.log<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Troubleshooting<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">I could not write error message to log<\/h3>\n\n\n\n<ul><li><strong>Cache problem<\/strong> &#8211; if you have added code snippet to .phtml there is a big chance that you need to clear a cache.<\/li><li><strong>File permission<\/strong> &#8211; make sure that Magento has write to files permission on the files located in  MAGE_DIR\/var\/log\/.<\/li><li><strong>Production mode<\/strong> &#8211; log records marked as DEBUG will not be written to file in production mode. Try to use different log level like INFO or ALERT.<\/li><li><strong>Disabled in configuration<\/strong> &#8211; Magento 2 has an options in configuration for enable\/disable logging. You can simple check this out here: <br><em>Stores &gt;&gt; Configuration &gt;&gt; Advanced &gt;&gt; Developer &gt;&gt; Debug &gt;&gt; Log to File<\/em><br><em>Stores &gt;&gt; Configuration &gt;&gt; Advanced &gt;&gt; Developer &gt;&gt; Syslog &gt;&gt; Log to Syslog<\/em><\/li><\/ul>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"671\" height=\"488\" src=\"https:\/\/www.magetrend.com\/blog\/wp-content\/uploads\/2019\/05\/magento-2-write-to-log-enable.jpg\" alt=\"\" class=\"wp-image-19\" srcset=\"https:\/\/www.magetrend.com\/blog\/wp-content\/uploads\/2019\/05\/magento-2-write-to-log-enable.jpg 671w, https:\/\/www.magetrend.com\/blog\/wp-content\/uploads\/2019\/05\/magento-2-write-to-log-enable-300x218.jpg 300w\" sizes=\"(max-width: 671px) 100vw, 671px\" \/><figcaption><em>Stores &gt;&gt; Configuration &gt;&gt; Advanced &gt;&gt; Developer<\/em><\/figcaption><\/figure><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Error after changes<\/h3>\n\n\n\n<ul><li>If you are editing controller file and Psr\\Log was included in controller constructor, it might be that you will get an error. But solution is simple. To fix this error, there is need to run Magento upgrade command.<\/li><\/ul>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>These all cases are base on my own experience working with Magento 2. If you had some other situation, when write to log just wasn&#8217;t working, let me know in the comments bellow. I will be more than happy to append this post with your case.<\/p>\n\n\n<div class=\"simple-container\"> <a class=\"simple-img\" href=\"https:\/\/www.magetrend.com\/magento-2-email-templates?utm_source=article&utm_medium=post_8&utm_campaign=pos2\"><img decoding=\"async\" src=\"\/blog\/wp-content\/uploads\/prod\/magento-2-email-templates.png\" \/><\/a> <div class=\"simple-row simple-title\"><a href=\"https:\/\/www.magetrend.com\/magento-2-email-templates?utm_source=article&utm_medium=post_8&utm_campaign=pos2\">Responsive Emails Templates for Magento 2<\/a><\/div> <div class=\"simple-row simple-text\"><a href=\"https:\/\/www.magetrend.com\/magento-2-email-templates?utm_source=article&utm_medium=post_8&utm_campaign=pos2\">Our #1 bestseller. With user-friendly template editor!<\/a><\/div> <div class=\"simple-action\"><a href=\"https:\/\/www.magetrend.com\/magento-2-email-templates?utm_source=article&utm_medium=post_8&utm_campaign=pos2\">Read More<\/a> <\/div> <\/div>\n","protected":false},"excerpt":{"rendered":"<p>The following code snippet is the alternative to Mage::log(&#8216;*&#8217;) logging method in Magento 1. It&#8217;s a good way to log the errors while you are debugging or in the other &hellip; <\/p>\n<div class=\"grid-footer\"><a href=\"https:\/\/www.magetrend.com\/blog\/magento-2-write-to-log\/\" class=\"more-link\">Read More <i class=\"mticon-arrow\"><\/i><\/a><\/div>\n","protected":false},"author":1,"featured_media":279,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/www.magetrend.com\/blog\/wp-json\/wp\/v2\/posts\/8"}],"collection":[{"href":"https:\/\/www.magetrend.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.magetrend.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.magetrend.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.magetrend.com\/blog\/wp-json\/wp\/v2\/comments?post=8"}],"version-history":[{"count":27,"href":"https:\/\/www.magetrend.com\/blog\/wp-json\/wp\/v2\/posts\/8\/revisions"}],"predecessor-version":[{"id":467,"href":"https:\/\/www.magetrend.com\/blog\/wp-json\/wp\/v2\/posts\/8\/revisions\/467"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.magetrend.com\/blog\/wp-json\/wp\/v2\/media\/279"}],"wp:attachment":[{"href":"https:\/\/www.magetrend.com\/blog\/wp-json\/wp\/v2\/media?parent=8"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.magetrend.com\/blog\/wp-json\/wp\/v2\/categories?post=8"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.magetrend.com\/blog\/wp-json\/wp\/v2\/tags?post=8"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}