Created
August 24, 2011 14:01
-
-
Save nrk/1168144 to your computer and use it in GitHub Desktop.
A simple handler for Monolog that publishes log entries to Redis channels.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require 'autoload.php'; | |
use Monolog\Logger; | |
use Nrk\Monolog\Handler\RedisPublishHandler; | |
$log = new Logger('test'); | |
$log->pushHandler(new RedisPublishHandler('tcp://127.0.0.1/')); | |
$log->addWarning('warning message'); | |
$log->addError('error message'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Nrk\Monolog\Handler; | |
use Predis\Client; | |
use Monolog\Logger; | |
use Monolog\Formatter\JsonFormatter; | |
use Monolog\Handler\AbstractProcessingHandler; | |
/** | |
* Simple handler that publishes log entries to Redis channels so that they can | |
* propagate to other clients subscribed to those channels. | |
* | |
* By default each log entry is JSON-encoded and then published to a channel | |
* following this naming scheme: $LOG_CHANNEL.$LOG_LEVEL. | |
* | |
* Clients can use SUBSCRIBE or PSUBSCRIBE to control which channels or log | |
* levels they are interested in consuming. | |
* | |
* @author Daniele Alessandri <[email protected]> | |
*/ | |
class RedisPublishHandler extends AbstractProcessingHandler | |
{ | |
private $_redis; | |
/** | |
* @param mixed $parameters Connection parameters to the Redis server or a client instance | |
* @param integer $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($parameters, $level = Logger::DEBUG, $bubble = true) | |
{ | |
parent::__construct($level, $bubble); | |
$this->_redis = $this->createConnection($parameters); | |
} | |
/** | |
* Initialize the client instance used to handle the connection to Redis. | |
* | |
* It is possible to use a different client library (e.g. phpredis) simply | |
* by overriding this method in a subclass. | |
* | |
* @param mixed $parameters Connection parameters to the Redis server or a client instance | |
* @return Predis\Client | |
*/ | |
protected function createConnection($parameters) | |
{ | |
return $parameters instanceof Client ? $parameters : new Client($parameters); | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
protected function getDefaultFormatter() | |
{ | |
return new JsonFormatter(); | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
protected function write(array $record) | |
{ | |
$channel = $this->formatChannel($record); | |
$this->_redis->publish($channel, $record['formatted']); | |
} | |
/** | |
* Generate from the log entry the name of the Redis channel that will be | |
* used by the PUBLISH command. | |
* | |
* @param array $record The record to handle | |
* @return string | |
*/ | |
protected function formatChannel(array $record) | |
{ | |
return $record['channel'] . '.' . strtolower($record['level_name']); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment