RedisHandler.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php declare(strict_types=1);
  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\Formatter\FormatterInterface;
  13. use Monolog\Logger;
  14. use Monolog\LogRecord;
  15. /**
  16. * Logs to a Redis key using rpush
  17. *
  18. * usage example:
  19. *
  20. * $log = new Logger('application');
  21. * $redis = new RedisHandler(new Predis\Client("tcp://localhost:6379"), "logs", "prod");
  22. * $log->pushHandler($redis);
  23. *
  24. * @author Thomas Tourlourat <thomas@tourlourat.com>
  25. */
  26. class RedisHandler extends AbstractProcessingHandler
  27. {
  28. /** @var \Predis\Client<\Predis\Client>|\Redis */
  29. private $redisClient;
  30. /** @var string */
  31. private $redisKey;
  32. /** @var int */
  33. protected $capSize;
  34. /**
  35. * @param \Predis\Client<\Predis\Client>|\Redis $redis The redis instance
  36. * @param string $key The key name to push records to
  37. * @param int $capSize Number of entries to limit list size to, 0 = unlimited
  38. */
  39. public function __construct($redis, string $key, $level = Logger::DEBUG, bool $bubble = true, int $capSize = 0)
  40. {
  41. if (!(($redis instanceof \Predis\Client) || ($redis instanceof \Redis))) {
  42. throw new \InvalidArgumentException('Predis\Client or Redis instance required');
  43. }
  44. $this->redisClient = $redis;
  45. $this->redisKey = $key;
  46. $this->capSize = $capSize;
  47. parent::__construct($level, $bubble);
  48. }
  49. /**
  50. * {@inheritDoc}
  51. */
  52. protected function write(LogRecord $record): void
  53. {
  54. if ($this->capSize) {
  55. $this->writeCapped($record);
  56. } else {
  57. $this->redisClient->rpush($this->redisKey, $record->formatted);
  58. }
  59. }
  60. /**
  61. * Write and cap the collection
  62. * Writes the record to the redis list and caps its
  63. */
  64. protected function writeCapped(LogRecord $record): void
  65. {
  66. if ($this->redisClient instanceof \Redis) {
  67. $mode = defined('\Redis::MULTI') ? \Redis::MULTI : 1;
  68. $this->redisClient->multi($mode)
  69. ->rpush($this->redisKey, $record->formatted)
  70. ->ltrim($this->redisKey, -$this->capSize, -1)
  71. ->exec();
  72. } else {
  73. $redisKey = $this->redisKey;
  74. $capSize = $this->capSize;
  75. $this->redisClient->transaction(function ($tx) use ($record, $redisKey, $capSize) {
  76. $tx->rpush($redisKey, $record->formatted);
  77. $tx->ltrim($redisKey, -$capSize, -1);
  78. });
  79. }
  80. }
  81. /**
  82. * {@inheritDoc}
  83. */
  84. protected function getDefaultFormatter(): FormatterInterface
  85. {
  86. return new LineFormatter();
  87. }
  88. }