1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ae\MonologFluentdBundle\Monolog\Handler; |
4
|
|
|
|
5
|
|
|
use Fluent\Logger\FluentLogger; |
6
|
|
|
use Monolog\Formatter\JsonFormatter; |
7
|
|
|
use Monolog\Handler\AbstractProcessingHandler; |
8
|
|
|
use Monolog\Logger; |
9
|
|
|
|
10
|
|
|
class FluentdHandler extends AbstractProcessingHandler |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var FluentLogger |
14
|
|
|
*/ |
15
|
|
|
protected $fluentLogger; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Initialize Handler. |
19
|
|
|
* |
20
|
|
|
* @param string $host |
21
|
|
|
* @param int $port |
22
|
|
|
* @param array $options FluentLogger options |
23
|
|
|
* @param int $level The minimum logging level at which this handler will be triggered |
24
|
|
|
* @param bool $bubble Whether the messages that are handled can bubble up the stack or not |
25
|
|
|
*/ |
26
|
2 |
|
public function __construct( |
27
|
|
|
$host = FluentLogger::DEFAULT_ADDRESS, |
28
|
|
|
$port = FluentLogger::DEFAULT_LISTEN_PORT, |
29
|
|
|
$options = array(), |
30
|
|
|
$level = Logger::DEBUG, |
31
|
|
|
$bubble = true) |
32
|
|
|
{ |
33
|
2 |
|
parent::__construct($level, $bubble); |
34
|
2 |
|
$this->fluentLogger = $this->makeFluentLogger($host, $port, $options); |
35
|
|
|
|
36
|
|
|
// By default FluentLogger would write to stderr for every message gone wrong. |
37
|
|
|
// We find it a bad default (you would probably start to log miriad of data as error). |
38
|
|
|
// You can reset the same or a different error handler by accessing the logger with getFluentLogger(); |
39
|
|
|
$this->fluentLogger->registerErrorHandler(function ($logger, $entity, $error) {}); |
|
|
|
|
40
|
2 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Get the internal FluentLogger instance. |
44
|
|
|
* |
45
|
|
|
* @return FluentLogger |
46
|
|
|
*/ |
47
|
|
|
public function getFluentLogger() |
48
|
|
|
{ |
49
|
|
|
return $this->fluentLogger; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Create a new instande of FluentLogger. |
54
|
|
|
* |
55
|
|
|
* @param string $host |
56
|
|
|
* @param int $port |
57
|
|
|
* @param array $options FluentLogger options |
58
|
|
|
*/ |
59
|
|
|
protected function makeFluentLogger($host, $port, $options = array()) |
60
|
|
|
{ |
61
|
|
|
return new FluentLogger($host, $port, $options); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* {@inheritdoc} |
66
|
|
|
*/ |
67
|
1 |
|
protected function write(array $record) |
68
|
|
|
{ |
69
|
1 |
|
$this->fluentLogger->post($this->formatTag($record), $record['context']); |
70
|
1 |
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Format a fluentd tag using the record data. |
74
|
|
|
* |
75
|
|
|
* @param array $record |
76
|
|
|
* |
77
|
|
|
* @return string the tag |
78
|
|
|
*/ |
79
|
1 |
|
protected function formatTag($record) |
80
|
|
|
{ |
81
|
1 |
|
return sprintf('%s.%s', $record['channel'], $record['message']); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* {@inheritdoc} |
86
|
|
|
*/ |
87
|
2 |
|
public function close() |
88
|
|
|
{ |
89
|
2 |
|
$this->fluentLogger->close(); |
90
|
2 |
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return \Monolog\Formatter\FormatterInterface |
94
|
|
|
*/ |
95
|
1 |
|
protected function getDefaultFormatter() |
96
|
|
|
{ |
97
|
1 |
|
return new JsonFormatter(); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.