|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* AppserverIo\Logger\Handlers\LogstashHandler |
|
5
|
|
|
* |
|
6
|
|
|
* NOTICE OF LICENSE |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the Open Software License (OSL 3.0) |
|
9
|
|
|
* that is available through the world-wide-web at this URL: |
|
10
|
|
|
* http://opensource.org/licenses/osl-3.0.php |
|
11
|
|
|
* |
|
12
|
|
|
* PHP version 5 |
|
13
|
|
|
* |
|
14
|
|
|
* @author Tim Wagner <[email protected]> |
|
15
|
|
|
* @copyright 2015 TechDivision GmbH <[email protected]> |
|
16
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
17
|
|
|
* @link http://github.com/appserver-io/logger |
|
18
|
|
|
* @link http://www.appserver.io |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
namespace AppserverIo\Logger\Handlers; |
|
22
|
|
|
|
|
23
|
|
|
use Psr\Log\LogLevel; |
|
24
|
|
|
use AppserverIo\Logger\LogMessageInterface; |
|
25
|
|
|
use AppserverIo\Logger\Formatters\JsonFormatter; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* A logstash handler implementation that sends the log message to |
|
29
|
|
|
* Logstash using a UDP connection. |
|
30
|
|
|
* |
|
31
|
|
|
* @author Tim Wagner <[email protected]> |
|
32
|
|
|
* @copyright 2015 TechDivision GmbH <[email protected]> |
|
33
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
34
|
|
|
* @link http://github.com/appserver-io/logger |
|
35
|
|
|
* @link http://www.appserver.io |
|
36
|
|
|
*/ |
|
37
|
|
|
class LogstashHandler extends DummyHandler |
|
38
|
|
|
{ |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* The logstash host. |
|
42
|
|
|
* |
|
43
|
|
|
* @var string |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $host; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* The logstash port. |
|
49
|
|
|
* |
|
50
|
|
|
* @var integer |
|
51
|
|
|
*/ |
|
52
|
|
|
protected $port; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Default constructor |
|
56
|
|
|
* |
|
57
|
|
|
* @param string $host The logstash host name/IP address |
|
58
|
|
|
* @param integer $port The logstash UDP port |
|
59
|
|
|
* @param integer $logLevel The minimum logging level at which this handler will be triggered |
|
60
|
|
|
*/ |
|
61
|
|
|
public function __construct($host = '127.0.0.1', $port = 9514, $logLevel = LogLevel::DEBUG) |
|
62
|
|
|
{ |
|
63
|
|
|
|
|
64
|
|
|
// call parent constructor |
|
65
|
|
|
parent::__construct($logLevel); |
|
66
|
|
|
|
|
67
|
|
|
// set the JSON formatter |
|
68
|
|
|
$this->formatter = new JsonFormatter(); |
|
69
|
|
|
|
|
70
|
|
|
// initialize the variables |
|
71
|
|
|
$this->host = $host; |
|
72
|
|
|
$this->port = $port; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Returns the lostash host. |
|
77
|
|
|
* |
|
78
|
|
|
* @return string The logstash host |
|
79
|
|
|
*/ |
|
80
|
|
|
protected function getHost() |
|
81
|
|
|
{ |
|
82
|
|
|
return $this->host; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Returns the logstash port. |
|
87
|
|
|
* |
|
88
|
|
|
* @return integer The logstash port |
|
89
|
|
|
*/ |
|
90
|
|
|
protected function getPort() |
|
91
|
|
|
{ |
|
92
|
|
|
return $this->port; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Handles the log message. |
|
97
|
|
|
* |
|
98
|
|
|
* @param \AppserverIo\Logger\LogMessageInterface $logMessage The message to be handled |
|
99
|
|
|
* |
|
100
|
|
|
* @return void |
|
101
|
|
|
*/ |
|
102
|
|
|
public function handle(LogMessageInterface $logMessage) |
|
103
|
|
|
{ |
|
104
|
|
|
if ($this->shouldLog($logMessage->getLevel())) { |
|
105
|
|
|
// check the log level |
|
106
|
|
|
|
|
107
|
|
|
// the JSON encoded log message |
|
108
|
|
|
$jsonMessage = $this->getFormatter()->format($logMessage); |
|
109
|
|
|
|
|
110
|
|
|
// initialize a UDP socket |
|
111
|
|
|
$socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); |
|
112
|
|
|
|
|
113
|
|
|
// connect, write the message and close the socket |
|
114
|
|
|
socket_connect($socket, $this->getHost(), $this->getPort()); |
|
115
|
|
|
socket_write($socket, $jsonMessage, strlen($jsonMessage)); |
|
116
|
|
|
socket_close($socket); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|