Writer.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * @file
  4. * Contains the Logger model.
  5. */
  6. namespace Redis\Logger;
  7. class Writer {
  8. const PREFIX = 'drupal:logger';
  9. /**
  10. * @var \Redis
  11. * Assumes use of Predis, not PhpRedis
  12. */
  13. protected $redis;
  14. /**
  15. * @var \Redis\Logger\Writer
  16. */
  17. protected static $instance;
  18. /**
  19. * TODO implement
  20. *
  21. * @var int
  22. * Maximum size of per-template event list. Non-empty() values enable
  23. * trimming on insert, so have a tiny performance impact: two O(1) commands
  24. * instead of one.
  25. */
  26. protected $list_limit = NULL;
  27. /**
  28. * @var bool
  29. * - true: messages are stored during the page cycle and sent on shutdown.
  30. * - false: message are sent immediately
  31. */
  32. protected $deferred = FALSE;
  33. /**
  34. * @var \Redis\Logger\Entry[]
  35. */
  36. protected $entries = array();
  37. /**
  38. * Constructor.
  39. *
  40. * @param \Redis $redis
  41. */
  42. protected function __construct(\Redis $redis) {
  43. $this->redis = $redis;
  44. }
  45. /**
  46. * Public method to build a Logger from the singleton Redis client.
  47. *
  48. * @return static
  49. */
  50. public static function createFromGlobals() {
  51. $redis = \Redis_Client::getClient();
  52. assert('$redis instanceof \Redis');
  53. return new static($redis);
  54. }
  55. /**
  56. * @return string[]
  57. */
  58. public function getChannels() {
  59. $templates = $this->getRawTemplates();
  60. }
  61. /**
  62. * @return int[]
  63. */
  64. public function getRawTemplates($minimum_level = WATCHDOG_DEBUG, $channel = '*') {
  65. $this->redis->getKeys(static::PREFIX . ":*:$channel:*");
  66. }
  67. /**
  68. * Logger singleton accessor.
  69. *
  70. * @return \Redis\Logger\Writer
  71. */
  72. public static function instance() {
  73. if (!isset(static::$instance)) {
  74. static::$instance = static::createFromGlobals();
  75. }
  76. return static::$instance;
  77. }
  78. /**
  79. * @param \Redis\Logger\Entry $entry
  80. */
  81. protected function post(Entry $entry) {
  82. }
  83. /**
  84. * @param \Redis\Logger\Entry $entry
  85. */
  86. public function log(Entry $entry) {
  87. if ($this->deferred) {
  88. $this->entries[] = $entry;
  89. }
  90. else {
  91. $this->post($entry);
  92. }
  93. }
  94. }