Tue, 02/09/2010 - 14:21
Log file viewer in PHP
<?php // Author : Hemanth.HM // Purpose : To view log file on the browser. function tail($file, $num_to_get=10) { $fp = fopen($file, 'r'); $position = filesize($file); fseek($fp, $position-1); $chunklen = 4096; while($position >= 0) { $position = $position - $chunklen; if ($position < 0) { $chunklen = abs($position); $position=0;} fseek($fp, $position); $data = fread($fp, $chunklen). $data; if (substr_count($data, "\n") >= $num_to_get + 1) { preg_match("!(.*?\n){".($num_to_get-1)."}$!", $data, $match); return $match[0]; } } fclose($fp); return $data; } function show_log($log){ $data=tail('$log'); $data = explode("\n",$data); while (list($var, $val) = each($data)) { ++$var; $val = trim($val); print "$val <br />"; } print "<hr>"; } <html> <body> <b>Click on the links to see the logs</b><br> <hr> <a href="?run=elog">Apache-Access-Log</a> <br> <a href="?run=aerr">Apache-Error-Log</a> <br> <a href="?run=0">Clear</a> <br> <hr> </body></html> <?php if (isset($_GET['run'])) $linkchoice=$_GET['run']; else $linkchoice=''; switch($linkchoice){ // Give path to log file as per your log requirement, must be have // proper permissions set for www-data group to read the log case 'alog' : show_log('/var/log/apache2/access.log'); break; case 'aerr' : show_log('/var/log/apache2/error.log'); break; } ?>
- Coding /
