开发者

PHP log parser(reading text files)

I have a log file assume it to be any text(txt) file.

I am reading it using php and performing functions.

The log file gets updated mostly every 10 seconds by a program as a normal log file does though the time interval is not fixed. I am ready to take some delay in showing the results.

One method is using cron jobs (which looks quite and odd to refresh the script every 10-20 seconds).

Assume the log file and the php file on the same server

I have my own dedicated server Ubuntu

Can anyone tell me a method through which i can read the file ?

Something like the php file gets executed whenever the file changes or do i have to use p开发者_如何学Cython/java or some other language for it ??

If the answer still sticks to cronjobs how do i add them in my Ubuntu server(i have php as a apache module) ?

Thanks


$fp = fopen('/path/to/log/file', 'r');
while (true) {
    $line = fgets($fp);
    if ($line === false) {
        echo "no new content, sleeping\n";
        sleep(3);
        fseek($fp, 0, SEEK_CUR);
    } else {
        echo $line;
    }
}


You can use:

# tail -f log.txt

or whatever the log file is named on the command line. This will keep the last few lines displayed in the terminal. You can also specify how many lines to display, like

# tail -n 100 -f log.txt
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜