tag:blogger.com,1999:blog-8472448211262689043.post6034315221298388094..comments2024-01-20T22:21:14.752-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's my current logging/debugging module:
...Here's my current logging/debugging module:<br /><br /> cached log file<br /> * @staticvar int $start => first time function was called during this request<br /> * @param bool $debug => true if debug<br /> * @param [multiple] $message => 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('%0.06f ms', $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('debug_file') : config('log_file');<br /> $open_type = 'a';<br /> $log_file = fopen($file_path, $open_type) or exit("Cannot open Log file: ".$file_path);<br /> }<br /> if ($print_header) {<br /> fwrite($log_file, "\n\nLog File: " . date('Y-m-d H:i:s') . "\n" .<br /> str_repeat('=', 30) . "\n");<br /> }<br /> for ($i = 0; $i < count($args); $i++) {<br /> $args[$i] = var_export($args[$i], true);<br /> }<br /> $message = ($args !== null && is_array($args)) ? implode("\t", $args) : $args;<br /> fwrite($log_file, '[' . $time . ']' . "\t" . $message . "\n");<br /> }<br />}Unknownhttps://www.blogger.com/profile/16211709183154664739noreply@blogger.com