GelfHandler.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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 Gelf\PublisherInterface;
  12. use Monolog\Logger;
  13. use Monolog\Formatter\GelfMessageFormatter;
  14. use Monolog\Formatter\FormatterInterface;
  15. /**
  16. * Handler to send messages to a Graylog2 (http://www.graylog2.org) server
  17. *
  18. * @author Matt Lehner <mlehner@gmail.com>
  19. * @author Benjamin Zikarsky <benjamin@zikarsky.de>
  20. */
  21. class GelfHandler extends AbstractProcessingHandler
  22. {
  23. /**
  24. * @var PublisherInterface|null the publisher object that sends the message to the server
  25. */
  26. protected $publisher;
  27. /**
  28. * @param PublisherInterface $publisher a publisher object
  29. * @param string|int $level The minimum logging level at which this handler will be triggered
  30. * @param bool $bubble Whether the messages that are handled can bubble up the stack or not
  31. */
  32. public function __construct(PublisherInterface $publisher, $level = Logger::DEBUG, bool $bubble = true)
  33. {
  34. parent::__construct($level, $bubble);
  35. $this->publisher = $publisher;
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. protected function write(array $record): void
  41. {
  42. $this->publisher->publish($record['formatted']);
  43. }
  44. /**
  45. * {@inheritDoc}
  46. */
  47. protected function getDefaultFormatter(): FormatterInterface
  48. {
  49. return new GelfMessageFormatter();
  50. }
  51. }