RedisHandler.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /*
  3. * This file is part of the Monolog package.
  4. *
  5. * (c) Jordi Boggiano <j.boggiano@seld.be>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Monolog\Handler;
  11. use Monolog\Formatter\LineFormatter;
  12. use Monolog\Logger;
  13. /**
  14. * Logs to a Redis key using rpush
  15. *
  16. * usage example:
  17. *
  18. * $log = new Logger('application');
  19. * $redis = new RedisHandler(new Predis\Client("tcp://localhost:6379"), "logs", "prod");
  20. * $log->pushHandler($redis);
  21. *
  22. * @author Thomas Tourlourat <thomas@tourlourat.com>
  23. */
  24. class RedisHandler extends AbstractProcessingHandler
  25. {
  26. private $redisClient;
  27. private $redisKey;
  28. protected $capSize;
  29. /**
  30. * @param \Predis\Client|\Redis $redis The redis instance
  31. * @param string $key The key name to push records to
  32. * @param int $level The minimum logging level at which this handler will be triggered
  33. * @param bool $bubble Whether the messages that are handled can bubble up the stack or not
  34. * @param int $capSize Number of entries to limit list size to
  35. */
  36. public function __construct($redis, $key, $level = Logger::DEBUG, $bubble = true, $capSize = false)
  37. {
  38. if (!(($redis instanceof \Predis\Client) || ($redis instanceof \Redis))) {
  39. throw new \InvalidArgumentException('Predis\Client or Redis instance required');
  40. }
  41. $this->redisClient = $redis;
  42. $this->redisKey = $key;
  43. $this->capSize = $capSize;
  44. parent::__construct($level, $bubble);
  45. }
  46. /**
  47. * {@inheritDoc}
  48. */
  49. protected function write(array $record)
  50. {
  51. if ($this->capSize) {
  52. $this->writeCapped($record);
  53. } else {
  54. $this->redisClient->rpush($this->redisKey, $record["formatted"]);
  55. }
  56. }
  57. /**
  58. * Write and cap the collection
  59. * Writes the record to the redis list and caps its
  60. *
  61. * @param array $record associative record array
  62. * @return void
  63. */
  64. protected function writeCapped(array $record)
  65. {
  66. if ($this->redisClient instanceof \Redis) {
  67. $this->redisClient->multi()
  68. ->rpush($this->redisKey, $record["formatted"])
  69. ->ltrim($this->redisKey, -$this->capSize, -1)
  70. ->exec();
  71. } else {
  72. $redisKey = $this->redisKey;
  73. $capSize = $this->capSize;
  74. $this->redisClient->transaction(function ($tx) use ($record, $redisKey, $capSize) {
  75. $tx->rpush($redisKey, $record["formatted"]);
  76. $tx->ltrim($redisKey, -$capSize, -1);
  77. });
  78. }
  79. }
  80. /**
  81. * {@inheritDoc}
  82. */
  83. protected function getDefaultFormatter()
  84. {
  85. return new LineFormatter();
  86. }
  87. }