Closed skynet closed 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.
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.
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?
yes, thanks, already implemented on my end. would you accept a PR?
@skynet if you can provide it in a Iterator or Traversable way will be wonderful.
Ok, thanks.
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?
@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.
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.