RedisHandler.php 2.9 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. /**
  15. * Logs to a Redis key using rpush
  16. *
  17. * usage example:
  18. *
  19. * $log = new Logger('application');
  20. * $redis = new RedisHandler(new Predis\Client("tcp://localhost:6379"), "logs", "prod");
  21. * $log->pushHandler($redis);
  22. *
  23. * @author Thomas Tourlourat <thomas@tourlourat.com>
  24. */
  25. class RedisHandler extends AbstractProcessingHandler
  26. {
  27. private $redisClient;
  28. private $redisKey;
  29. protected $capSize;
  30. /**
  31. * @param \Predis\Client|\Redis $redis The redis instance
  32. * @param string $key The key name to push records to
  33. * @param int $level The minimum logging level at which this handler will be triggered
  34. * @param bool $bubble Whether the messages that are handled can bubble up the stack or not
  35. * @param int $capSize Number of entries to limit list size to, 0 = unlimited
  36. */
  37. public function __construct($redis, string $key, $level = Logger::DEBUG, bool $bubble = true, int $capSize = 0)
  38. {
  39. if (!(($redis instanceof \Predis\Client) || ($redis instanceof \Redis))) {
  40. throw new \InvalidArgumentException('Predis\Client or Redis instance required');
  41. }
  42. $this->redisClient = $redis;
  43. $this->redisKey = $key;
  44. $this->capSize = $capSize;
  45. parent::__construct($level, $bubble);
  46. }
  47. /**
  48. * {@inheritDoc}
  49. */
  50. protected function write(array $record)
  51. {
  52. if ($this->capSize) {
  53. $this->writeCapped($record);
  54. } else {
  55. $this->redisClient->rpush($this->redisKey, $record["formatted"]);
  56. }
  57. }
  58. /**
  59. * Write and cap the collection
  60. * Writes the record to the redis list and caps its
  61. *
  62. * @param array $record associative record array
  63. * @return void
  64. */
  65. protected function writeCapped(array $record)
  66. {
  67. if ($this->redisClient instanceof \Redis) {
  68. $this->redisClient->multi()
  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. }