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

Already on GitHub? Sign in to your account

Added zeromq handler #147

Closed
wants to merge 11 commits into
from

Conversation

Projects
None yet
7 participants

Simple handler which allows logging into a zeromq socket :)

Contributor

stof commented Jan 15, 2013

you should add tests for this class and add the handler in the list in the readme

I added a simple test and completed the readme, what do you think? Thx

I guess you mean that ZMQ is not installed if the class doesn´t exist?

You guess correctly :) It's the same method the test for the ampq handler deals with the possibility that the needed php extension is not installed, so i figured it's ok..

Ah, now i see what you meant.. ;) Typo is now fixed..

@Seldaek Seldaek and 1 other commented on an outdated diff Jan 22, 2013

src/Monolog/Handler/ZmqHandler.php
+ *
+ * @author Marius Krämer <marius.kraemer@mercoline.de>
+ */
+class ZmqHandler extends AbstractProcessingHandler
+{
+ /**
+ * @var ZMQSocket $socket
+ */
+ protected $socket;
+
+ /**
+ * @param ZMQSocket $socket Connected zeromq socket
+ * @param int $level The minimum logging level at which this handler will be triggered
+ * @param bool $bubble Whether the messages that are handled can bubble up the stack or not
+ */
+ public function __construct(ZMQSocket $socket, $level = Logger::DEBUG, $bubble = true)
@Seldaek

Seldaek Jan 22, 2013

Owner

I am wondering if it wouldn't be best to pass the ZMQContext here, plus a host/port string and a persistendId defaulting to monolog. That way the socket would only be created/connected on demand. I am not sure how expensive it is, but in general all monolog handlers try to do things lazily when possible since in production in many cases nothing gets logged.

@Seldaek

Seldaek Jan 22, 2013

Owner

Looking at the docs some more, I would say the ZMQContext might even be made optional and created with sane defaults (1, true seems good) if not given.

@mkraemer

mkraemer Jan 22, 2013

Something like this? Seems not too expensive, as long as $persistId is not null only a single connection is created..
I'm not sure how to unit test this though ;)

/**
 * @param string $dsn       DSN to the endpoint
 * @param mixed  $type      ZMQ::SOCKET_ constant
 * @param mixed  $persistId String for persistent connections, null to disable persistent connections
 * @param int    $level     The minimum logging level at which this handler will be triggered
 * @param bool   $bubble    Whether the messages that are handled can bubble up the stack or not
 */
public function __construct($dsn, $type = ZMQ::SOCKET_PUSH, $persistId = 'monolog', $level = Logger::DEBUG, $bubble = true)
{
    $this->dsn = $dsn;
    $this->type = $type;
    $this->persistId = $persistId;

    parent::__construct($level, $bubble);
}

/**
 * {@inheritDoc}
 */
protected function write(array $record)
{
    $context = new ZMQContext();

    $socket = $context->getSocket($this->type, $this->persistId);

    $socket->connect($this->dsn);

    $socket->send($record['formatted'], ZMQ::MODE_NOBLOCK);
}

Any thoughts?

@Seldaek

Seldaek Jan 22, 2013

Owner

I would mean more like this (which allows testability):

public function __construct($dsn, $persistId = 'monolog', $type = ZMQ::SOCKET_PUSH, $context = null, $level = Logger::DEBUG, $bubble = true)
{
    $this->dsn = $dsn;
    $this->persistId = $persistId;
    $this->type = $type;
    $this->context = $context;

    parent::__construct($level, $bubble);
}

/**
 * {@inheritDoc}
 */
protected function write(array $record)
{
    if (null === $this->context) {
        $this->context = new ZMQContext(1, true);
    }
    if (null === $this->socket) {
        $this->socket = $this->context->getSocket($this->type, $this->persistId);
        $this->socket->connect($this->dsn);
    }

    $this->socket->send($record['formatted'], ZMQ::MODE_NOBLOCK);
}

AlexButter and others added some commits Jan 16, 2013

Option to use SSL for PushoverHandler (enabled by default), and close…
… connection after sending a message to PushOver.

Solves #146: When sending messages in rapid succession to pushover.net only the first one is sent.
The SSL option is needed when sending messages to users that are not the pushover.net app owner; pushover.net doesn't accept those messages over plain HTTP.
Make ChromePHPHandler and FirePHPHandler sendHeaders flag static so t…
…hat when multiple instances of the handler class exist they will all respect the one-time check that determines whether the handler should append its logs to the response header

mkraemer commented Feb 4, 2013

What's the benefit of passing both a context and letting the handler create the socket?

The only two use cases i can imagine are:

  • dsn, socket type and persistId are passed in, context and socket are created by the handler
  • a ready-made socket is passed in and used

I don't see any use in creating and passing the context manually, but letting the handler create the socket.

Thx :)

Owner

Seldaek commented Feb 11, 2013

@mkraemer well, that might be a better approach. I am not so familiar with ZMQ so my suggestion might be crap :)

Any news on this PR?

mathroc commented Dec 30, 2013

👍

@Seldaek Seldaek added the Feature label Mar 23, 2014

Owner

Seldaek commented May 26, 2016

Closing as it's very old, it can always be done as an third party handler package if anyone really wants.

@Seldaek Seldaek closed this May 26, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment