kritya
kritya

Reputation: 3372

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 python/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

Upvotes: 0

Views: 2812

Answers (2)

Alex Howansky
Alex Howansky

Reputation: 53636

$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;
    }
}

Upvotes: 1

serialworm
serialworm

Reputation: 733

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

Upvotes: 0

Related Questions