123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- <?php
- /**
- * @file
- * Contains the Logger model.
- */
- namespace Redis\Logger;
- class Writer {
- const PREFIX = 'drupal:logger';
- /**
- * @var \Redis
- * Assumes use of Predis, not PhpRedis
- */
- protected $redis;
- /**
- * @var \Redis\Logger\Writer
- */
- protected static $instance;
- /**
- * TODO implement
- *
- * @var int
- * Maximum size of per-template event list. Non-empty() values enable
- * trimming on insert, so have a tiny performance impact: two O(1) commands
- * instead of one.
- */
- protected $list_limit = NULL;
- /**
- * @var bool
- * - true: messages are stored during the page cycle and sent on shutdown.
- * - false: message are sent immediately
- */
- public $deferred = FALSE;
- /**
- * @var \Redis\Logger\Entry[]
- */
- protected $entries = array();
- /**
- * @var \Redis\Logger\Settings
- */
- protected $settings;
- /**
- * Constructor.
- *
- * @param \Redis $redis
- */
- protected function __construct(\Redis $redis, Settings $settings = NULL) {
- $this->redis = $redis;
- if (!isset($settings)) {
- $settings = new Settings();
- }
- $this->settings = $settings;
- }
- /**
- * Public method to build a Logger from the singleton Redis client.
- *
- * @return static
- */
- public static function createFromGlobals() {
- $redis = \Redis_Client::getClient();
- assert('$redis instanceof \Redis');
- return new static($redis);
- }
- /**
- * @return string[]
- */
- public function getChannels() {
- $templates = $this->getRawTemplates();
- }
- /**
- * @return int[]
- */
- public function getRawTemplates($minimum_level = WATCHDOG_DEBUG, $channel = '*') {
- $this->redis->getKeys(static::PREFIX . ":*:$channel:*");
- }
- /**
- * Logger singleton accessor.
- *
- * @return \Redis\Logger\Writer
- */
- public static function instance() {
- if (!isset(static::$instance)) {
- static::$instance = static::createFromGlobals();
- }
- return static::$instance;
- }
- /**
- * @param \Redis\Logger\Entry $entry
- */
- protected function post(Entry $entry) {
- $list = $this->settings->getPrefix() . "{$entry->type}:{$entry->severity}:{$entry->message}";
- $item = serialize($entry);
- $this->redis->lPush($list, $item);
- }
- /**
- * @param \Redis\Logger\Entry $entry
- */
- public function log(Entry $entry) {
- if ($this->deferred) {
- $this->entries[] = $entry;
- }
- else {
- $this->post($entry);
- }
- }
- }
|