kassner / log-parser

PHP Web Server Log Parser Library
Apache License 2.0
338 stars 64 forks source link

Pointer #14

Closed skynet closed 9 years ago

skynet commented 9 years ago

Is it possible to enable a pointer, so that when running this with crond at subsequent cycle it doesn't read the entire log file again? Thanks.

kassner commented 9 years ago

I think this library is not in charge of such thing. This means managing and storing information about previous files parsed, but here we just convert a single line into a stdClass object.

skynet commented 9 years ago

No, it's actually parsing an entire file, subject to having new items appended. Knowing that there will be new lines in my log file after just a couple of seconds I need to know where to start to parse, at subsequent iteration.

kassner commented 9 years ago

I think you could do something like this with fopen, ftell and fseek:

$h = fopen('/var/log/nginx/access.log', 'r');

while (!feof($h)) {
   $parser->parse(fgets($h));
}

$lastPosition = ftell($h);
fclose($f);

Store the $lastPosition wherever you want and resume the parsing with fseek($h, $lastPosition) before the while on the example above.

This solves your problem?

skynet commented 9 years ago

yes, thanks, already implemented on my end. would you accept a PR?

kassner commented 9 years ago

@skynet if you can provide it in a Iterator or Traversable way will be wonderful.

skynet commented 9 years ago

Ok, thanks.

MACscr commented 8 years ago

Hope you dont mind me commenting on this, but how would this work when the log file is logrotated? Wouldnt that cause a problem with the $lastPosition?

kassner commented 8 years ago

@MACscr It probably will cause some unexpected behaviour, but I never looked into this, I usually don't parse the current access.log file, just old ones.