1<?php declare(strict_types=1);
2
3/*
4 * This file is part of the Monolog package.
5 *
6 * (c) Jordi Boggiano <j.boggiano@seld.be>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12namespace Monolog\Handler;
13
14use Gelf\PublisherInterface;
15use Monolog\Logger;
16use Monolog\Formatter\GelfMessageFormatter;
17use Monolog\Formatter\FormatterInterface;
18
19/**
20 * Handler to send messages to a Graylog2 (http://www.graylog2.org) server
21 *
22 * @author Matt Lehner <mlehner@gmail.com>
23 * @author Benjamin Zikarsky <benjamin@zikarsky.de>
24 */
25class GelfHandler extends AbstractProcessingHandler
26{
27    /**
28     * @var PublisherInterface the publisher object that sends the message to the server
29     */
30    protected $publisher;
31
32    /**
33     * @param PublisherInterface $publisher a gelf publisher object
34     */
35    public function __construct(PublisherInterface $publisher, $level = Logger::DEBUG, bool $bubble = true)
36    {
37        parent::__construct($level, $bubble);
38
39        $this->publisher = $publisher;
40    }
41
42    /**
43     * {@inheritDoc}
44     */
45    protected function write(array $record): void
46    {
47        $this->publisher->publish($record['formatted']);
48    }
49
50    /**
51     * {@inheritDoc}
52     */
53    protected function getDefaultFormatter(): FormatterInterface
54    {
55        return new GelfMessageFormatter();
56    }
57}
58