{"id":3910,"date":"2021-11-21T22:47:25","date_gmt":"2021-11-21T22:47:25","guid":{"rendered":"https:\/\/www.itersdesktop.com\/?p=3910"},"modified":"2021-11-21T23:14:19","modified_gmt":"2021-11-21T23:14:19","slug":"how-to-write-logging-messages-to-a-file-in-php","status":"publish","type":"post","link":"https:\/\/www.itersdesktop.com\/fr\/2021\/11\/21\/how-to-write-logging-messages-to-a-file-in-php\/","title":{"rendered":"How to write logging messages to a file in PHP"},"content":{"rendered":"<p>Being proficient in logging debug or error messages is critical to every programmer. If you used to make programs in any high and robust programming languages such as Java or C#, logging to PHP is boring but it is good enough to capture messages to support your debugging. We are going to discover how to log messages in PHP applications in a minute.<\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_83 counter-hierarchy ez-toc-counter ez-toc-light-blue ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a14f45d64596\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a14f45d64596\"  aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.itersdesktop.com\/fr\/2021\/11\/21\/how-to-write-logging-messages-to-a-file-in-php\/#introduction-to-the-error-log-function\" >Introduction to the error_log() function<\/a><ul class='ez-toc-list-level-2' ><li class='ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.itersdesktop.com\/fr\/2021\/11\/21\/how-to-write-logging-messages-to-a-file-in-php\/#syntax\" >Syntax<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.itersdesktop.com\/fr\/2021\/11\/21\/how-to-write-logging-messages-to-a-file-in-php\/#parameters\" >Parameters<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.itersdesktop.com\/fr\/2021\/11\/21\/how-to-write-logging-messages-to-a-file-in-php\/#a-toy-project\" >A toy project<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.itersdesktop.com\/fr\/2021\/11\/21\/how-to-write-logging-messages-to-a-file-in-php\/#more-readings\" >More readings<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.itersdesktop.com\/fr\/2021\/11\/21\/how-to-write-logging-messages-to-a-file-in-php\/#in-summary\" >In summary<\/a><\/li><\/ul><\/nav><\/div>\n<h1><span class=\"ez-toc-section\" id=\"introduction-to-the-error-log-function\"><\/span>Introduction to the error_log() function<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p>The primary function we use to send messages to a defined stream is error_log. As explained in <a href=\"https:\/\/www.php.net\/manual\/en\/function.error-log.php\">its documentation<\/a>, the function will send an error message to the server&rsquo;s error log or a physical file.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"syntax\"><\/span>Syntax<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The syntax of the function is defined as below:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">error_log(\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 string $message,\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 int $message_type = 0,\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 string $destination = ?,\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 string $extra_headers = ?): bool;<\/pre>\n<h2><span class=\"ez-toc-section\" id=\"parameters\"><\/span>Parameters<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><code class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">$message<\/code><br \/>\nThe message should be logged.<\/p>\n<p><code class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">$message_type<\/code><br \/>\nsays where the error should go. It could be any integer value between 0 and 4. Please read the official documentation before implementing your code.<\/p>\n<p><code class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">$destination<\/code><br \/>\nsays whether the message is written out. It depends on the <code class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">message_type<\/code> parameter as described above.<\/p>\n<h1><span class=\"ez-toc-section\" id=\"a-toy-project\"><\/span>A toy project<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p>To demonstrate how the function works, I have pushed a simple project in <a href=\"https:\/\/github.com\/ITersDesktop\">the blog&rsquo;s GitHub<\/a> at the <a href=\"https:\/\/github.com\/ITersDesktop\/phplog\">repository<\/a>. You can clone it to test the function quickly.<\/p>\n<p>The program writes out a timestamped message to a file on the root directory of the application every loading the index.php. It shouldn&rsquo;t be simple like that in reality. You have to customise your implementation to log error messages instead of dumping anything. An example of the snippet looks like below.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">&lt;?php\r\nuse \\Datetime;\r\n\r\n$message = \"\r\n&lt;h2&gt;Hello, I am a newbie with PHP programming. Please help me!!!&lt;\/h2&gt;\r\n&lt;p&gt;See the log file has been newly created un the root directory of this file.&lt;\/p&gt;\r\n\";\r\necho $message;\r\n\r\n$data = array('shopid' =&gt; 3,'version' =&gt; 1, 'value' =&gt; 2);  \/\/here $data is dummy variable\r\n$log_file = $_SERVER['DOCUMENT_ROOT'].\"\/errors-\".date(\"Y-n-j\").\".log\";\r\ndate_default_timezone_set('Europe\/London');\r\n$date = date('m\/d\/Y H:i:s', time());\r\n$error_message = \"[\".$date.\"] DEBUG: \".json_encode($data).\"\\n\";\r\n\r\nerror_log(print_r($error_message,true), 3, $log_file);\r\n\r\n\/\/ In $data we can mention the error message and create the log<\/pre>\n<figure id=\"attachment_3912\" aria-describedby=\"caption-attachment-3912\" style=\"width: 1024px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.itersdesktop.com\/2021\/11\/21\/how-to-write-logging-messages-to-a-file-in-php\/3910-write-log-messages-to-file\/\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-3912 size-large\" src=\"https:\/\/www.itersdesktop.com\/wp-content\/uploads\/2021\/11\/3910-write-log-messages-to-file-1024x576.png\" alt=\"How to write log messages to files in PHP\" width=\"1024\" height=\"576\" srcset=\"https:\/\/www.itersdesktop.com\/wp-content\/uploads\/2021\/11\/3910-write-log-messages-to-file-1024x576.png 1024w, https:\/\/www.itersdesktop.com\/wp-content\/uploads\/2021\/11\/3910-write-log-messages-to-file-300x169.png 300w, https:\/\/www.itersdesktop.com\/wp-content\/uploads\/2021\/11\/3910-write-log-messages-to-file-768x432.png 768w, https:\/\/www.itersdesktop.com\/wp-content\/uploads\/2021\/11\/3910-write-log-messages-to-file-1536x864.png 1536w, https:\/\/www.itersdesktop.com\/wp-content\/uploads\/2021\/11\/3910-write-log-messages-to-file.png 1920w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption id=\"caption-attachment-3912\" class=\"wp-caption-text\"><em>How to write log messages to files in PHP<\/em><\/figcaption><\/figure>\n<h1><span class=\"ez-toc-section\" id=\"more-readings\"><\/span>More readings<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<ol>\n<li>Error Handling and Logging, <a href=\"https:\/\/www.php.net\/manual\/en\/book.errorfunc.php,\">https:\/\/www.php.net\/manual\/en\/book.errorfunc.php,<\/a> accessed on 18\/11\/2021<\/li>\n<li>Getting started quickly with PHP logging, <a href=\"https:\/\/www.scalyr.com\/blog\/getting-started-quickly-with-php-logging\">https:\/\/www.scalyr.com\/blog\/getting-started-quickly-with-php-logging<\/a>, accessed on 21\/11\/2021<\/li>\n<\/ol>\n<h1><span class=\"ez-toc-section\" id=\"in-summary\"><\/span>In summary<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p>We have gone through how to use the error_log function to write out debug and error messages to file systems. If you find the post useful to your work, please help us circulate it. Thank you for your support.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Being proficient in logging debug or error messages is critical to every programmer. If you used to make programs in any high and robust programming languages such as Java or&hellip; <\/p>\n","protected":false},"author":2,"featured_media":3912,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[806,4,222],"tags":[758,621,89],"class_list":["post-3910","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php","category-web-engineering","category-web-programming","tag-log","tag-logging","tag-php"],"_links":{"self":[{"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/posts\/3910","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/comments?post=3910"}],"version-history":[{"count":4,"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/posts\/3910\/revisions"}],"predecessor-version":[{"id":3915,"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/posts\/3910\/revisions\/3915"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/media\/3912"}],"wp:attachment":[{"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/media?parent=3910"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/categories?post=3910"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/tags?post=3910"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}