log-parser icon indicating copy to clipboard operation
log-parser copied to clipboard

Seek on a file pointer

Open skynet opened this issue 9 years ago • 4 comments

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.

skynet avatar Jan 03 '16 01:01 skynet

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 :)

mvar avatar Jan 04 '16 06:01 mvar

This is perfect, I implemented it as a hack but will do with this piece of code and test. Thanks!

skynet avatar Jan 04 '16 14:01 skynet

Glad it helped.

What is your use case? Do you want to get log messages in nearly real-time?

mvar avatar Jan 04 '16 14:01 mvar

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!

skynet avatar Jan 05 '16 00:01 skynet