Jon
Jon

Reputation: 437854

Logging stream filter for PHP?

I need to debug a socket connection that my PHP frontend makes to a backend service on another host, and I need to do this as close to the metal as possible. I 've already abstracted the connection at various levels, which among others gives me the capability to easily attach stream filters to the connection. So the problem should be very easy to solve: package a stream filter into a class which allows arbitrary callbacks to be executed when data is sent or received, and then for example append these to a couple of log files.

The only trouble is that my (naive?) expectation that there should be some such filter implementation floating online appears to be wrong! I don't mind writing the filter myself, but maybe there is something available that I just didn't manage to find?

For reference, I did google many obvious variations on "php logging stream filter".

Update: To clarify, what I 'm looking for is something that would allow me to write code morally equivalent to this:

$params = array(
    'onDataSent' => function($data) { echo "Sent: $data\n"; },
    'onDataReceived' => function($data) { echo "Received: $data\n"; },
);

stream_filter_register('logging', 'HookableStreamFilter');
stream_filter_append($someStream, 'logging', STREAM_FILTER_ALL, $params);

Upvotes: 4

Views: 801

Answers (1)

Jon
Jon

Reputation: 437854

For posterity, here's what I ended whipping up. The filter itself derives from php_user_filter, which is described in the docs for stream_filter_register:

class HookableFilter extends php_user_filter {
    public function filter($in, $out, &$consumed, $closing) {
        $data = '';

        while ($bucket = stream_bucket_make_writeable($in)) {
            $consumed += $bucket->datalen;
            $data .= $bucket->data;
            stream_bucket_append($out, $bucket);
        }

        call_user_func($this->params, $data);
        return PSFS_PASS_ON;
    }
}

Note that I 'm using directly $this->params as the callback, making this really spartan in function (there's a reason; see below).

Registering the filter with PHP:

stream_filter_register('generic.hookable', 'HookableFilter');

Attaching the filter to the stream:

$callback = function($data) { /* log the data */ };
stream_filter_append($stream, 'generic.hookable', STREAM_FILTER_READ, $callback);
stream_filter_append($stream, 'generic.hookable', STREAM_FILTER_WRITE, $callback);

Important: As far as I was able to see, if you attach a filter to both channels of a duplex stream (e.g. one produced with stream_socket_client) there is no way for your filter to know on which channel it's operating when it is invoked. Therefore if you need to differentiate between incoming and outgoing data as I was, the only option is to attach the filter separately to each channel. If you do, you will also certainly want to supply a different callback for each channel (not the same as done in the simplified example above).

Upvotes: 1

Related Questions