Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Seek on a file pointer #1

Open
skynet opened this issue Jan 3, 2016 · 4 comments
Open

Seek on a file pointer #1

skynet opened this issue Jan 3, 2016 · 4 comments

Comments

@skynet
Copy link

skynet commented Jan 3, 2016

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.

@mvar
Copy link
Owner

mvar commented Jan 4, 2016

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

@skynet
Copy link
Author

skynet commented Jan 4, 2016

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

@mvar
Copy link
Owner

mvar commented Jan 4, 2016

Glad it helped.

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

@skynet
Copy link
Author

skynet commented Jan 5, 2016

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants