Welcome Guest, Not a member yet? Register   Sign In
HTTP Error log reader
#1

[eluser]louis w[/eluser]
I was in the need of a front-end error log reader to help quick debugging of client sites. This script is useful for an easy way to browse the recent error messages without having to ssh in to the server (i have the system folder set at inaccessible to http requests). It's VERY bare bones, and just reads the file and outputs it to screen.

I am sure there is a lot of additional features that could be added to it. I just threw it together and thought i would share.




Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
&lt;html &gt;
&lt;head&gt;
    &lt;title&gt;Error Log&lt;/title&gt;
    &lt;meta http-equiv="Content-type" content="text/html; charset=utf-8" /&gt;
    &lt;meta name="MSSmartTagsPreventParsing" content="true" /&gt;
    &lt;style&gt;
        
        BODY {
            color: #666;
            font-family: Lucida Grande, Verdana, Sans-serif;
            font-size: 11px;
            }
        
        PRE {
            background-color: black;
            font-size: 12px;
            line-height: 14px;
            color: white;
            padding: 20px;
            }
    
    &lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;?php

define("TEXT_FILE", dirname(__FILE__) . '/system/logs/log-' . date('Y-m-d') . '.php');
define("LINES_COUNT", 50);

function read_file($file, $lines) {
    //global $fsize;
    $handle = fopen($file, "r");
    $linecounter = $lines;
    $pos = -2;
    $beginning = false;
    $text = array();
    while ($linecounter > 0) {
        $t = " ";
        while ($t != "\n") {
            if(fseek($handle, $pos, SEEK_END) == -1) {
                $beginning = true;
                break;
            }
            $t = fgetc($handle);
            $pos --;
        }
        $linecounter --;
        if ($beginning) {
            rewind($handle);
        }
        $text[$lines-$linecounter-1] = fgets($handle);
        if ($beginning) break;
    }
    fclose ($handle);
    return array_reverse($text);
}

echo "<strong>".TEXT_FILE."</strong>";

if (file_exists(TEXT_FILE)) {

    $fsize = round(filesize(TEXT_FILE)/1024/1024,2);

    echo "<br/>File size is {$fsize} megabytes";
    echo "<br/>Last ".LINES_COUNT." lines of the file:";

    echo '<pre>';

    $lines = read_file(TEXT_FILE, LINES_COUNT);
    foreach ($lines as $line) {
        echo $line;
    }

    echo '</pre>';

    echo '<div class="refresh_time">Page rendered at:  <strong>' . date('Y-m-d H:i:s') . '</strong></div>';

} else {

    echo '<div style="color: red;">ERROR: File not found</div>';

}?&gt;

&lt;/body&gt;
&lt;/html&gt;




Theme © iAndrew 2016 - Forum software by © MyBB