|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Msschl\Monolog\Handler; |
|
4
|
|
|
|
|
5
|
|
|
use Http\Client\HttpClient; |
|
6
|
|
|
use Http\Discovery\HttpClientDiscovery; |
|
7
|
|
|
use Http\Discovery\MessageFactoryDiscovery; |
|
8
|
|
|
use Http\Message\MessageFactory; |
|
9
|
|
|
use Monolog\Formatter\FormatterInterface; |
|
10
|
|
|
use Monolog\Formatter\JsonFormatter; |
|
11
|
|
|
use Monolog\Handler\AbstractProcessingHandler; |
|
12
|
|
|
use Monolog\Logger; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* This file is part of the msschl\monolog-http-handler package. |
|
16
|
|
|
* |
|
17
|
|
|
* Copyright (c) 2018 Markus Schlotbohm |
|
18
|
|
|
* |
|
19
|
|
|
* For the full copyright and license information, please view the LICENSE.md |
|
20
|
|
|
* file that was distributed with this source code. |
|
21
|
|
|
*/ |
|
22
|
|
|
class HttpHandler extends AbstractProcessingHandler |
|
23
|
|
|
{ |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* The http client instance. |
|
27
|
|
|
* |
|
28
|
|
|
* @var \Http\Client\HttpClient |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $client; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* The message factory instance. |
|
34
|
|
|
* |
|
35
|
|
|
* @var \Http\Message\MessageFactory |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $messageFactory; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* The options array. |
|
41
|
|
|
* |
|
42
|
|
|
* @var array |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $options = [ |
|
45
|
|
|
'uri' => null, |
|
46
|
|
|
'method' => 'GET', |
|
47
|
|
|
'headers' => [ |
|
48
|
|
|
'Content-Type' => 'application/json' |
|
49
|
|
|
], |
|
50
|
|
|
'protocolVersion' => '1.1' |
|
51
|
|
|
]; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Create a new monolog http client handler instance. |
|
55
|
|
|
* |
|
56
|
|
|
* @param array $options The array of options consisting of the uri, method, headers and protocol |
|
57
|
|
|
* version. |
|
58
|
|
|
* @param HttpClient|null $client An instance of a psr-7 http client implementation or null when the |
|
59
|
|
|
* HttpClientDiscovery should be used to find an instance. |
|
60
|
|
|
* @param MessageFactory|null $factory An instance of a psr-7 message factory implementation or null when |
|
61
|
|
|
* the MessageFactoryDiscovery should be used to find an instance. |
|
62
|
|
|
* @param int $level The minimum logging level at which this handler will be triggered. |
|
63
|
|
|
* @param boolean $bubble Whether the messages that are handled can bubble up the stack or not. |
|
64
|
|
|
*/ |
|
65
|
|
|
public function __construct( |
|
66
|
|
|
array $options = [], |
|
67
|
|
|
HttpClient $client = null, |
|
68
|
|
|
MessageFactory $factory = null, |
|
69
|
|
|
$level = Logger::DEBUG, |
|
70
|
|
|
$bubble = true |
|
71
|
|
|
) { |
|
72
|
|
|
$this->client = $client ?: HttpClientDiscovery::find(); |
|
73
|
|
|
$this->messageFactory = $factory ?: MessageFactoryDiscovery::find(); |
|
74
|
|
|
|
|
75
|
|
|
$this->setOptions($options); |
|
76
|
|
|
|
|
77
|
|
|
parent::__construct($level, $bubble); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Sets the options for the monolog http handler. |
|
82
|
|
|
* |
|
83
|
|
|
* @param array $options |
|
84
|
|
|
* @return \Msschl\Monolog\Handler\HttpHandler |
|
85
|
|
|
*/ |
|
86
|
|
|
public function setOptions(array $options) |
|
87
|
|
|
{ |
|
88
|
|
|
$this->options = array_merge($this->options, $options); |
|
89
|
|
|
|
|
90
|
|
|
return $this; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Sets the uri. |
|
95
|
|
|
* |
|
96
|
|
|
* @param string|null $uri |
|
97
|
|
|
* @return \Msschl\Monolog\Handler\HttpHandler |
|
98
|
|
|
*/ |
|
99
|
|
|
public function setUri(string $uri = null) |
|
100
|
|
|
{ |
|
101
|
|
|
$this->options['uri'] = $uri; |
|
102
|
|
|
|
|
103
|
|
|
return $this; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Gets the uri. |
|
108
|
|
|
* |
|
109
|
|
|
* @return string|null |
|
110
|
|
|
*/ |
|
111
|
|
|
public function getUri() |
|
112
|
|
|
{ |
|
113
|
|
|
return $this->options['uri']; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Sets the http method. |
|
118
|
|
|
* |
|
119
|
|
|
* @param string $method |
|
120
|
|
|
* @return \Msschl\Monolog\Handler\HttpHandler |
|
121
|
|
|
*/ |
|
122
|
|
|
public function setMethod(string $method) |
|
123
|
|
|
{ |
|
124
|
|
|
$this->options['method'] = $method; |
|
125
|
|
|
|
|
126
|
|
|
return $this; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Gets the http method. |
|
131
|
|
|
* |
|
132
|
|
|
* @return string |
|
133
|
|
|
*/ |
|
134
|
|
|
public function getMethod() : string |
|
135
|
|
|
{ |
|
136
|
|
|
return $this->options['method'] ?: 'GET'; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Sets the headers. |
|
141
|
|
|
* |
|
142
|
|
|
* @param array $headers |
|
143
|
|
|
* @return \Msschl\Monolog\Handler\HttpHandler |
|
144
|
|
|
*/ |
|
145
|
|
|
public function setHeaders(array $headers) |
|
146
|
|
|
{ |
|
147
|
|
|
$this->options['headers'] = $headers; |
|
148
|
|
|
|
|
149
|
|
|
return $this; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Gets the headers. |
|
154
|
|
|
* |
|
155
|
|
|
* @return array |
|
156
|
|
|
*/ |
|
157
|
|
|
public function getHeaders() : array |
|
158
|
|
|
{ |
|
159
|
|
|
return $this->options['headers'] ?: [ 'Content-Type' => 'application/json' ]; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Sets the http protocol version. |
|
164
|
|
|
* |
|
165
|
|
|
* @param string $version |
|
166
|
|
|
* @return \Msschl\Monolog\Handler\HttpHandler |
|
167
|
|
|
*/ |
|
168
|
|
|
public function setProtocolVersion(string $version = '1.1') |
|
169
|
|
|
{ |
|
170
|
|
|
$this->options['protocolVersion'] = $version; |
|
171
|
|
|
|
|
172
|
|
|
return $this; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Gets the http protocol version. |
|
177
|
|
|
* |
|
178
|
|
|
* @return string |
|
179
|
|
|
*/ |
|
180
|
|
|
public function getProtocolVersion() : string |
|
181
|
|
|
{ |
|
182
|
|
|
return $this->options['protocolVersion'] ?: '1.1'; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Handles a set of records at once. |
|
187
|
|
|
* |
|
188
|
|
|
* @param array $records The records to handle (an array of record arrays) |
|
189
|
|
|
* @return bool |
|
190
|
|
|
*/ |
|
191
|
|
|
public function handleBatch(array $records) |
|
192
|
|
|
{ |
|
193
|
|
|
foreach ($records as $key => $record) { |
|
194
|
|
|
if ($this->isHandling($record)) { |
|
195
|
|
|
$record = $this->processRecord($record); |
|
196
|
|
|
$records['records'][] = $record; |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
unset($records[$key]); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
$records['formatted'] = $this->getFormatter()->formatBatch($records['records'] ?? []); |
|
203
|
|
|
|
|
204
|
|
|
$this->write($records); |
|
205
|
|
|
|
|
206
|
|
|
return false === $this->bubble; |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* Gets the default formatter. |
|
211
|
|
|
* |
|
212
|
|
|
* @return \Monolog\Formatter\JsonFormatter |
|
213
|
|
|
*/ |
|
214
|
|
|
protected function getDefaultFormatter() : FormatterInterface |
|
215
|
|
|
{ |
|
216
|
|
|
return new JsonFormatter(); |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
|
|
* Returns the HTTP adapter. |
|
221
|
|
|
* |
|
222
|
|
|
* @return \Http\Client\HttpClient |
|
223
|
|
|
*/ |
|
224
|
|
|
protected function getHttpClient(): HttpClient |
|
225
|
|
|
{ |
|
226
|
|
|
return $this->client; |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
/** |
|
230
|
|
|
* Returns the message factory. |
|
231
|
|
|
* |
|
232
|
|
|
* @return \Http\Message\MessageFactory |
|
233
|
|
|
*/ |
|
234
|
|
|
protected function getMessageFactory(): MessageFactory |
|
235
|
|
|
{ |
|
236
|
|
|
return $this->messageFactory; |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* Writes the record. |
|
241
|
|
|
* |
|
242
|
|
|
* @param array $record |
|
243
|
|
|
* @return void |
|
244
|
|
|
*/ |
|
245
|
|
|
protected function write(array $record) |
|
246
|
|
|
{ |
|
247
|
|
|
$uri = $this->getUri(); |
|
248
|
|
|
|
|
249
|
|
|
if (empty($uri)) { |
|
250
|
|
|
return; |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
$request = $this->getMessageFactory()->createRequest( |
|
254
|
|
|
$this->getMethod(), |
|
255
|
|
|
$this->getUri(), |
|
256
|
|
|
$this->getHeaders(), |
|
257
|
|
|
$record['formatted'], |
|
258
|
|
|
$this->getProtocolVersion() |
|
259
|
|
|
); |
|
260
|
|
|
|
|
261
|
|
|
try { |
|
262
|
|
|
$this->getHttpClient()->sendRequest($request); |
|
263
|
|
|
} catch (\Exception $e) { |
|
264
|
|
|
// QUESTION(msschl): How to handle the thrown exceptions??? |
|
265
|
|
|
return; |
|
266
|
|
|
} |
|
267
|
|
|
} |
|
268
|
|
|
} |