GelfHandler.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 Publisher the publisher object that sends the message to the server
  25. */
  26. protected $publisher;
  27. /**
  28. * @param PublisherInterface $publisher a publisher object
  29. * @param 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, $bubble = true)
  33. {
  34. parent::__construct($level, $bubble);
  35. $this->publisher = $publisher;
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function close()
  41. {
  42. $this->publisher = null;
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. protected function write(array $record)
  48. {
  49. $this->publisher->publish($record['formatted']);
  50. }
  51. /**
  52. * {@inheritDoc}
  53. */
  54. protected function getDefaultFormatter(): FormatterInterface
  55. {
  56. return new GelfMessageFormatter();
  57. }
  58. }