|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Bouncer package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) François Hodierne <[email protected]> |
|
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 Bouncer\Logger; |
|
13
|
|
|
|
|
14
|
|
|
use Monolog\Formatter\LogstashFormatter; |
|
15
|
|
|
use Monolog\Handler\StreamHandler; |
|
16
|
|
|
use Monolog\Logger; |
|
17
|
|
|
|
|
18
|
|
|
class LogstashLogger extends BaseLogger |
|
19
|
|
|
{ |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var string |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $host; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var int |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $port; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var string |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $protocol; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var string |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $channel; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var string |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $type; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var object|bool |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $logger; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @var int |
|
53
|
|
|
*/ |
|
54
|
|
|
protected $timeout; |
|
55
|
|
|
|
|
56
|
|
|
public function __construct($host, $port, $protocol = 'udp', $channel = 'bouncer', $type = 'access_log', $timeout = 2) |
|
57
|
|
|
{ |
|
58
|
|
|
$this->host = $host; |
|
59
|
|
|
$this->port = $port; |
|
60
|
|
|
$this->protocol = $protocol; |
|
61
|
|
|
$this->channel = $channel; |
|
62
|
|
|
$this->type = $type; |
|
63
|
|
|
$this->timeout = $timeout; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* {@inheritDoc} |
|
68
|
|
|
*/ |
|
69
|
|
|
public function log(array $logEntry) |
|
70
|
|
|
{ |
|
71
|
|
|
$message = (string)$logEntry['request']; |
|
72
|
|
|
|
|
73
|
|
|
$entry = $this->format($logEntry); |
|
74
|
|
|
|
|
75
|
|
|
$logger = $this->getLogger(); |
|
76
|
|
|
if ($logger) { |
|
77
|
|
|
$logger->info($message, $entry); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @return object|null |
|
83
|
|
|
*/ |
|
84
|
|
|
public function getLogger() |
|
85
|
|
|
{ |
|
86
|
|
|
if (isset($this->logger)) { |
|
87
|
|
|
return $this->logger; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
$logger = new Logger($this->channel); |
|
91
|
|
|
|
|
92
|
|
|
$formatter = new LogstashFormatter( |
|
93
|
|
|
$this->type, $systemName = null, $extraPrefix = null, $contextPrefix = '', LogstashFormatter::V1); |
|
94
|
|
|
|
|
95
|
|
|
$stream = stream_socket_client("{$this->protocol}://{$this->host}:{$this->port}", $errno, $errstr, $this->timeout); |
|
96
|
|
|
if (!$stream) { |
|
97
|
|
|
error_log("Unable to connect to '{$this->protocol}://{$this->host}': {$errstr} ({$errno})"); |
|
98
|
|
|
return $this->logger = false; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
$streamHandler = new StreamHandler($stream); |
|
102
|
|
|
$streamHandler->setFormatter($formatter); |
|
103
|
|
|
$logger->pushHandler($streamHandler); |
|
104
|
|
|
|
|
105
|
|
|
return $this->logger = $logger; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
} |
|
109
|
|
|
|