Menu

Log watching

Anonymous
2012-05-29
2012-12-12
  • Anonymous

    Anonymous - 2012-05-29

    Hi Guys,

    I managed to read log files. I know its not the way how it should looks like, but maybe one of you can use it, or can manage to make it right.

    PHP:

    <?php
            /**
         * read a log file last lines
         *
         * Author: László Simon
         *
         * @return html formed string
         */
    function log_watch($log, $onlyerrors = true, $lines = 100, $block_size = 4096, $showRealLineNumbers = false) {
    $out = '';
    $a = last_lines($log, $lines+1, 512);
    $s = true;
    $i=$lines+1;
    if ($showRealLineNumbers) $i = count_lines($log, 4096)-1;
    $color_err = '#FF0000';
    $color_warn = 'yellow';
    $color_exc = '#FF6600';
    $color_fail = 'orange';
    $out .= '<div class="logtitle"><span>'.$log.'<small style="float:right">(shows the last '.$lines.' lines in reverse order)</small></span>';
    $out .= '<div class="scrollContent"><table class="scrollTable">
    <tbody class="scrollContent">';
    foreach ($a as $v) {
        $tr_bgcolor = '';
        $type = '';
        if ( $a[0] != $v) {
            if (!$onylerrors || (strpos($v,'Error') || strpos($v,'Exception') || strpos($v,'Warning') || strpos($v,'Failed'))) {
        //'Error, Exception, Warning, Failed
    
                if (strpos($v,'Error')) {$tr_bgcolor = 'style="background-color:'.$tr_bgcolor.$color_err.';"'; $type='Error';} //strpos is not case sensitive!
                if (strpos($v,'Exception')) {$tr_bgcolor = 'style="background-color:'.$tr_bgcolor.$color_exc.';"'; $type='Exception';}
                if (strpos($v,'Warning')) {$tr_bgcolor = 'style="background-color:'.$tr_bgcolor.$color_warn.';"'; $type='Warning';}
                if (strpos($v,'Failed')) {$tr_bgcolor = 'style="background-color:'.$tr_bgcolor.$color_fail.';"'; $type='Failed';}
    
                if ($s) $out .= '<tr class="normalRow">';
                else    $out .= '<tr class="alternateRow">';
    
                $out .= '<td>';
                $out .= $i;
                $out .= '</td>';
                $out .= '<td>';
                $out .= $type;
                $out .= '</td>';
                $out .= '<td '.$tr_bgcolor.'>';
                $out .= $v;
                $out .= '</td>';
                $out .= '</tr>';
                }
        }
        $i-=1;
        $s=!$s;
        }
    
        $out .= '</tbody>
        </table>
        </div></div>';
    
        return $out;
    }
    function last_lines($path, $line_count, $block_size = 512){
        $lines = array();
        // we will always have a fragment of a non-complete line
        // keep this in here till we have our next entire line.
        $leftover = "";
        $fh = fopen($path, 'r');
        // go to the end of the file
        fseek($fh, 0, SEEK_END);
        do{
            // need to know whether we can actually go back
            // $block_size bytes
            $can_read = $block_size;
            if(ftell($fh) < $block_size){
                $can_read = ftell($fh);
            }
            // go back as many bytes as we can
            // read them to $data and then move the file pointer
            // back to where we were.
            fseek($fh, -$can_read, SEEK_CUR);
            $data = fread($fh, $can_read);
            $data .= $leftover;
            fseek($fh, -$can_read, SEEK_CUR);
            // split lines by \n. Then reverse them,
            // now the last line is most likely not a complete
            // line which is why we do not directly add it, but
            // append it to the data read the next time.
            $split_data = array_reverse(explode("\n", $data));
            $new_lines = array_slice($split_data, 0, -1);
            $lines = array_merge($lines, $new_lines);
            $leftover = $split_data[count($split_data) - 1];
        }
        while(count($lines) < $line_count && ftell($fh) != 0);
        if(ftell($fh) == 0){
            $lines[] = $leftover;
        }
        fclose($fh);
        // Usually, we will read too many lines, correct that here.
        return array_slice($lines, 0, $line_count);
    }
    function count_lines($path, $block_size = 4096) {
    $file = $path;
    $linecount = 0;
    $handle = fopen($file, "r");
    while(!feof($handle)){
      $line = fgets($handle, $block_size);
      $linecount = $linecount + substr_count($line, PHP_EOL);
    }
    fclose($handle);
    return $linecount;
    }
    ?>
    

    CSS:

    tbody.scrollContent td, tbody.scrollContent tr.normalRow td {
        color: #000;
        background: #FFF;
        border-bottom: none;
        border-left: none;
        border-right: 1px solid #CCC;
        border-top: 1px solid #DDD;
        padding: 2px 3px 3px 4px;
        font: normal normal 12px Courier New, Geneva, Arial, Helvetica, sans-serif
    }
    tbody.scrollContent tr.alternateRow td {
        color: #000;
        background: #EEE;
        border-bottom: none;
        border-left: none;
        border-right: 1px solid #CCC;
        border-top: 1px solid #DDD;
        padding: 2px 3px 3px 4px;
        font: normal normal 12px Courier New, Geneva, Arial, Helvetica, sans-serif
    }
    div.logtitle {
    background-color:#EDEDED;
    font-family: Verdana, "Bitstream Vera Sans";
    font-weight: bold;
    font-size: 100%;
    color: #2B2828;
    }
    div.scrollContent {
    width:100%;
    height:250px;
    overflow: scroll;
    }
    table.scrollTable {
    width:100%;
    height: 250px;
    padding: 0px;
    font-size: 16px;
    border:0;
    padding:0;
    border-spacing:0;
    }
    #logs {
        width: 915px;
    }
    

    Code into index_dynamic.html:

     <div id="logs"><h2><span>Logs</span></h2>
          <?php include '/test/readlog.php'; echo log_watch("C:\HB\HeartBeatMonitor.log",false,100,4096,true); ?>
    </div>
    

    For me its perfect, I hope it will help you also.

     
  • Erkan

    Erkan - 2012-05-29

    Hi,

    I think it's a great idea of plugin for the next version, thanks.

    We can also used the tail command (on Linux)

     
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.