log-parser
log-parser copied to clipboard
Seek on a file pointer
Is it possible to implement seek on a file pointer? This is needed to parse continuously updating log files, where items have been added since last parse.
You could implement it by extending LogIterator
class. Something like this:
class SeekableIterator extends LogIterator
{
/**
* @var resource
*/
private $fileHandler;
/**
* @var int
*/
private $seek;
/**
* @param int $seek
*/
public function setSeek($seek)
{
$this->seek = $seek;
}
/**
* {@inheritdoc}
*/
protected function getFileHandler()
{
if ($this->fileHandler !== null) {
return $this->fileHandler;
}
$this->fileHandler = parent::getFileHandler();
if ($this->seek !== null) {
fseek($this->fileHandler, $this->seek);
}
return $this->fileHandler;
}
}
If you think it's worth to have it built-in in the package let me know. Or create a Pull Request :)
This is perfect, I implemented it as a hack but will do with this piece of code and test. Thanks!
Glad it helped.
What is your use case? Do you want to get log messages in nearly real-time?
I am actually using NGINX to collect webhook calls from a mailing application (http://www.activecampaign.com/api/webhooks.php). Running the parser every minute from a cronjob I take the logs entries and send them to an InfluxDB database and then get them on a Grafana dashboard. I'll probably open source the whole thing. Thanks for your help!