| 103 |
- |
1 |
<?php
|
|
|
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 |
|
|
|
12 |
namespace Monolog\Handler;
|
|
|
13 |
|
|
|
14 |
use Gelf\IMessagePublisher;
|
|
|
15 |
use Gelf\PublisherInterface;
|
|
|
16 |
use Gelf\Publisher;
|
|
|
17 |
use InvalidArgumentException;
|
|
|
18 |
use Monolog\Logger;
|
|
|
19 |
use Monolog\Formatter\GelfMessageFormatter;
|
|
|
20 |
|
|
|
21 |
/**
|
|
|
22 |
* Handler to send messages to a Graylog2 (http://www.graylog2.org) server
|
|
|
23 |
*
|
|
|
24 |
* @author Matt Lehner <mlehner@gmail.com>
|
|
|
25 |
* @author Benjamin Zikarsky <benjamin@zikarsky.de>
|
|
|
26 |
*/
|
|
|
27 |
class GelfHandler extends AbstractProcessingHandler
|
|
|
28 |
{
|
|
|
29 |
/**
|
|
|
30 |
* @var Publisher the publisher object that sends the message to the server
|
|
|
31 |
*/
|
|
|
32 |
protected $publisher;
|
|
|
33 |
|
|
|
34 |
/**
|
|
|
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
|
|
|
38 |
*/
|
|
|
39 |
public function __construct($publisher, $level = Logger::DEBUG, $bubble = true)
|
|
|
40 |
{
|
|
|
41 |
parent::__construct($level, $bubble);
|
|
|
42 |
|
|
|
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');
|
|
|
45 |
}
|
|
|
46 |
|
|
|
47 |
$this->publisher = $publisher;
|
|
|
48 |
}
|
|
|
49 |
|
|
|
50 |
/**
|
|
|
51 |
* {@inheritdoc}
|
|
|
52 |
*/
|
|
|
53 |
public function close()
|
|
|
54 |
{
|
|
|
55 |
$this->publisher = null;
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
/**
|
|
|
59 |
* {@inheritdoc}
|
|
|
60 |
*/
|
|
|
61 |
protected function write(array $record)
|
|
|
62 |
{
|
|
|
63 |
$this->publisher->publish($record['formatted']);
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
/**
|
|
|
67 |
* {@inheritDoc}
|
|
|
68 |
*/
|
|
|
69 |
protected function getDefaultFormatter()
|
|
|
70 |
{
|
|
|
71 |
return new GelfMessageFormatter();
|
|
|
72 |
}
|
|
|
73 |
}
|