tag:blogger.com,1999:blog-8472448211262689043.post6034315221298388094..comments2026-01-31T00:32:51.812-08:00Comments on Advanced PHP Tutorial: Write to a log file with PHPLearnphphttp://www.blogger.com/profile/15934446588226257926noreply@blogger.comBlogger1125tag:blogger.com,1999:blog-8472448211262689043.post-41186053570295537302011-01-12T21:47:27.095-08:002011-01-12T21:47:27.095-08:00Here&#39;s my current logging/debugging module: ...Here&#39;s my current logging/debugging module:<br /><br /> cached log file<br /> * @staticvar int $start =&gt; first time function was called during this request<br /> * @param bool $debug =&gt; true if debug<br /> * @param [multiple] $message =&gt; any number of variables|strings, to be displayed<br /> */<br />function logger($debug, $message) {<br /> static $log_file;<br /> static $start = 0;<br /><br /> $print_header = ($start == 0);<br /> if ($start == 0) {<br /> $start = microtime(true);<br /> $time = 0;<br /> } else {<br /> $time = microtime(true) - $start;<br /> }<br /> $time = sprintf(&#39;%0.06f ms&#39;, $time);<br /> $args = func_get_args();<br /> $debug = $args[0];<br /> array_shift($args);<br /><br /> if(!empty($args)) {<br /> if(!$log_file) {<br /> $file_path = APP_PATH . ($debug) ? config(&#39;debug_file&#39;) : config(&#39;log_file&#39;);<br /> $open_type = &#39;a&#39;;<br /> $log_file = fopen($file_path, $open_type) or exit(&quot;Cannot open Log file: &quot;.$file_path);<br /> }<br /> if ($print_header) {<br /> fwrite($log_file, &quot;\n\nLog File: &quot; . date(&#39;Y-m-d H:i:s&#39;) . &quot;\n&quot; .<br /> str_repeat(&#39;=&#39;, 30) . &quot;\n&quot;);<br /> }<br /> for ($i = 0; $i &lt; count($args); $i++) {<br /> $args[$i] = var_export($args[$i], true);<br /> }<br /> $message = ($args !== null &amp;&amp; is_array($args)) ? implode(&quot;\t&quot;, $args) : $args;<br /> fwrite($log_file, &#39;[&#39; . $time . &#39;]&#39; . &quot;\t&quot; . $message . &quot;\n&quot;);<br /> }<br />}Unknownhttps://www.blogger.com/profile/16211709183154664739noreply@blogger.com