{"id":1436,"date":"2018-01-19T19:22:50","date_gmt":"2018-01-19T19:22:50","guid":{"rendered":"http:\/\/goofy-trucks.flywheelsites.com\/file-based-custom-logging-page-2\/"},"modified":"2018-01-19T19:24:47","modified_gmt":"2018-01-19T19:24:47","slug":"file-based-custom-logging-page-2","status":"publish","type":"post","link":"https:\/\/phpbuilder.com\/file-based-custom-logging-page-2\/","title":{"rendered":"File based, custom logging Page 2"},"content":{"rendered":"<div class=\"phpbuilder-content\">\n<div class=\"phpbuilder-meta\">\n<div class=\"\">By John Starkey<\/div>\n<div class=\"\">on September 30, 2002<\/div>\n<\/p><\/div>\n<div id=\"overflow-content\">\n<h2>How It&#8217;s Done<\/h2>\n<div class=\"articlePara\">\nFirst we declare two variables containing the info for our log file and the date in our desired<br \/>\nformat. This will make things easier if we need to change the location, the filename and\/or the<br \/>\ndate format at a later time. In this example I will use \/tmp\/article.log because I am using my<br \/>\ndevelopment machine. Please, do not use \/tmp on a production machine. Instead, have your admin s<br \/>\net up a directory outside of the DocumentRoot that the server can write to, and you can read.<br \/>\n\/tmp is normally chmod 777, meaning that anyone can read, write or execute from that directory.<br \/>\nThis can be a major security hazard.<\/div>\n<div class=\"articlePara\">\nWith that said, we declare the variables:<\/div>\n<div class=\"articlePhpEx\">\n<font face=\"courier\"><code><span style=\"color: #000000\"><\/p>\n<p><span style=\"color: #0000BB\">&lt;?php<br \/>\n<br \/>$log_file\u00a0<\/span><span style=\"color: #007700\">=\u00a0<\/span><span style=\"color: #DD0000\">'\/tmp\/article.log'<\/span><span style=\"color: #007700\">;<br \/>\n<br \/><\/span><span style=\"color: #0000BB\">$date\u00a0<\/span><span style=\"color: #007700\">=\u00a0<\/span><span style=\"color: #0000BB\">date<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #DD0000\">'Y-m-d\u00a0H:i:s\u00a0(T)'<\/span><span style=\"color: #007700\">);<br \/>\n<br \/><\/span><span style=\"color: #0000BB\">?&gt;<br \/>\n<br \/><\/span><br \/>\n<\/span><br \/>\n<\/code><\/font><\/div>\n<div class=\"articlePara\">\nNext, we create a function for handling the errors. To keep things simple, I will provide a<br \/>\nvery basic example. Of course, you are free to expand on this as you wish:<\/div>\n<div class=\"articlePhpEx\">\n<font face=\"courier\"><code><span style=\"color: #000000\"><\/p>\n<p><span style=\"color: #0000BB\">&lt;?php<br \/>\n<br \/><\/span><span style=\"color: #007700\">function\u00a0<\/span><span style=\"color: #0000BB\">simplisticErrorHandler<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$errno<\/span><span style=\"color: #007700\">,\u00a0<\/span><span style=\"color: #0000BB\">$errmsg<\/span><span style=\"color: #007700\">,\u00a0<\/span><span style=\"color: #0000BB\">$filename<\/span><span style=\"color: #007700\">,\u00a0<\/span><span style=\"color: #0000BB\">$linenum<\/span><span style=\"color: #007700\">,\u00a0<\/span><span style=\"color: #0000BB\">$vars<\/span><span style=\"color: #007700\">)<br \/>\n<br \/>{<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0global\u00a0<\/span><span style=\"color: #0000BB\">$log_file<\/span><span style=\"color: #007700\">,\u00a0<\/span><span style=\"color: #0000BB\">$date<\/span><span style=\"color: #007700\">;<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0if\u00a0(\u00a0<\/span><span style=\"color: #0000BB\">$errno\u00a0<\/span><span style=\"color: #007700\">==\u00a0<\/span><span style=\"color: #0000BB\">E_USER_NOTICE\u00a0<\/span><span style=\"color: #007700\">)<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0{<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #0000BB\">$error\u00a0<\/span><span style=\"color: #007700\">=\u00a0<\/span><span style=\"color: #DD0000\">\"$date\u00a0-\u00a0\"<\/span><span style=\"color: #007700\">.<\/span><span style=\"color: #0000BB\">$_SERVER<\/span><span style=\"color: #007700\">[<\/span><span style=\"color: #DD0000\">'REMOTE_ADDR'<\/span><span style=\"color: #007700\">].<\/span><span style=\"color: #DD0000\">\"\u00a0-\u00a0NOTICE:\u00a0$errmsg\u00a0in\u00a0$filename\u00a0on\u00a0line\u00a0$linenumn\"<\/span><span style=\"color: #007700\">;<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #0000BB\">error_log<\/span><span style=\"color: #007700\">(\u00a0<\/span><span style=\"color: #0000BB\">$error<\/span><span style=\"color: #007700\">,\u00a0<\/span><span style=\"color: #0000BB\">3<\/span><span style=\"color: #007700\">,\u00a0<\/span><span style=\"color: #0000BB\">$log_file\u00a0<\/span><span style=\"color: #007700\">);<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0}<br \/>\n<br \/>}<br \/>\n<br \/><\/span><span style=\"color: #0000BB\">?&gt;<br \/>\n<br \/><\/span><br \/>\n<\/span><br \/>\n<\/code><\/font><\/div>\n<div class=\"articlePara\">\nThis function will handle only the errors that you trigger and is pretty cut and dry, so I won&#8217;t<br \/>\ngo into detail. If you are having a bit of an issue it&#8217;s probably best that you drop back a little,<br \/>\nuntil you are a little more familiar with PHP. <\/div>\n<div class=\"articlePara\">\nThe key player in this function is error_log(), which takes 3 arguments: the message you want logged,<br \/>\nthe way we want the error handled and finally the destination. The first argument is pretty self-explanatory,<br \/>\nas is the third. The second argument determines how PHP handles logging the file and, taken from<br \/>\nhttp:\/\/www.php.net\/error_log, our current options are:<\/div>\n<blockquote>\n<div class=\"articlePara\">\n&#8211; 0 message is sent to PHP&#8217;s system logger, using the Operating System&#8217;s system logging mechanism or a file, depending on what the error_log configuration directive is set to. <br \/>\n&#8211; 1 message is sent by email to the address in the destination parameter. This is the only message type where the fourth parameter, extra_headers is used. This message type uses the same internal function as mail() does. <br \/>\n&#8211; 2 message is sent through the PHP debugging connection. This option is only available if remote debugging has been enabled. In this case, the destination parameter specifies the host name or IP address and optionally, port number, of the socket receiving the debug information. <br \/>\n&#8211; 3 message is appended to the file destination.<\/div>\n<\/blockquote>\n<div class=\"articlePara\">\nSo, naturally, in our case #3 is our destination of choice.<\/div>\n<\/div>\n<p><\/p>\n<div style=\"float: left; padding:15px; color:#17AAF3\">\n<div style=\"float:left; padding:2px;\"><a class=\"paginationPageLink\" href=\"starkey20020930.html\">\u00ab Previous Page<\/a><\/div>\n<div style=\"float:left; padding:2px 4px 2px 4px;\"><a class=\"pageNumber\" href=\"starkey20020930.html\">1<\/a> <\/div>\n<div style=\"float:left; font-size:16px; color:#FF7A22; padding:2px 2px 2px 2px; \">| <\/div>\n<div style=\"background-color:#B6E5FC; font-size:16px; margin-top:1px; padding:1px 4px 1px 4px; color:#000; font-style:bold; float:left;\">2<\/div>\n<div style=\"float:left; font-size:16px; color:#FF7A22; padding:2px 2px 2px 2px; \">| <\/div>\n<div style=\"float:left; padding:2px 4px 2px 4px;\"><a class=\"pageNumber\" href=\"starkey200209309ba9.html?page=3\">3<\/a> <\/div>\n<div style=\"float:left; font-size:16px; color:#FF7A22; padding:2px 2px 2px 2px; \">| <\/div>\n<div style=\"float:left; padding:2px 4px 2px 4px;\"><a class=\"pageNumber\" href=\"starkey20020930fdb0.html?page=4\">4<\/a> <\/div>\n<div style=\"float:left; font-size:16px; color:#FF7A22; padding:2px 2px 2px 2px; \">| <\/div>\n<div style=\"float:left; padding:2px 4px 2px 4px;\"><a class=\"pageNumber\" href=\"starkey20020930af4d.html?page=5\">5<\/a> <\/div>\n<div style=\"float:left; padding:2px;\"><a class=\"paginationPageLink\" href=\"starkey200209309ba9.html?page=3\">Next Page \u00bb<\/a><\/div>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>In my opinion, the best thing about working in the development phase is the liberty to throw your errors to the browser, without too many worries<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-1436","post","type-post","status-publish","format-standard","hentry","category-tutorials"],"_links":{"self":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1436","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/comments?post=1436"}],"version-history":[{"count":1,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1436\/revisions"}],"predecessor-version":[{"id":3294,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1436\/revisions\/3294"}],"wp:attachment":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/media?parent=1436"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/categories?post=1436"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/tags?post=1436"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}