4 * This file is part of the Monolog package.
6 * (c) Jordi Boggiano <j.boggiano@seld.be>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Monolog\Handler;
14 use Gelf\IMessagePublisher;
15 use Gelf\PublisherInterface;
17 use InvalidArgumentException;
19 use Monolog\Formatter\GelfMessageFormatter;
22 * Handler to send messages to a Graylog2 (http://www.graylog2.org) server
24 * @author Matt Lehner <mlehner@gmail.com>
25 * @author Benjamin Zikarsky <benjamin@zikarsky.de>
27 class GelfHandler extends AbstractProcessingHandler
30 * @var Publisher the publisher object that sends the message to the server
35 * @param PublisherInterface|IMessagePublisher|Publisher $publisher a publisher object
36 * @param int $level The minimum logging level at which this handler will be triggered
37 * @param bool $bubble Whether the messages that are handled can bubble up the stack or not
39 public function __construct($publisher, $level = Logger::DEBUG, $bubble = true)
41 parent::__construct($level, $bubble);
43 if (!$publisher instanceof Publisher && !$publisher instanceof IMessagePublisher && !$publisher instanceof PublisherInterface) {
44 throw new InvalidArgumentException('Invalid publisher, expected a Gelf\Publisher, Gelf\IMessagePublisher or Gelf\PublisherInterface instance');
47 $this->publisher = $publisher;
53 public function close()
55 $this->publisher = null;
61 protected function write(array $record)
63 $this->publisher->publish($record['formatted']);
69 protected function getDefaultFormatter()
71 return new GelfMessageFormatter();