1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Msschl\Monolog\Handler; |
4
|
|
|
|
5
|
|
|
use Http\Client\HttpClient; |
6
|
|
|
use Http\Message\MessageFactory; |
7
|
|
|
use Monolog\Formatter\FormatterInterface; |
8
|
|
|
use Monolog\Logger; |
9
|
|
|
use Msschl\Monolog\Formatter\SeqBaseFormatter; |
10
|
|
|
use Msschl\Monolog\Formatter\SeqCompactJsonFormatter; |
11
|
|
|
use Msschl\Monolog\Handler\HttpHandler; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* This file is part of the msschl\monolog-seq-handler package. |
15
|
|
|
* |
16
|
|
|
* Copyright (c) 2018 Markus Schlotbohm |
17
|
|
|
* |
18
|
|
|
* For the full copyright and license information, please view the LICENSE.md |
19
|
|
|
* file that was distributed with this source code. |
20
|
|
|
*/ |
21
|
|
|
class SeqHandler extends HttpHandler |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The uri to the logging endpoint of a seq-server. |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
const SEQ_API_URI = 'api/events/raw'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* The http method to the logging endpoint of a seq-server. |
33
|
|
|
* |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
const SEQ_API_METHOD = 'POST'; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* The options array. |
40
|
|
|
* |
41
|
|
|
* @var array |
42
|
|
|
*/ |
43
|
|
|
protected $options = [ |
44
|
|
|
'uri' => null, |
45
|
|
|
'method' => SeqHandler::SEQ_API_METHOD, |
46
|
|
|
'headers' => [ |
47
|
|
|
'Content-Type' => 'application/vnd.serilog.clef' |
48
|
|
|
], |
49
|
|
|
'protocolVersion' => '1.1' |
50
|
|
|
]; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Initializes a new instance of the {@see SeqHandler} class. |
54
|
|
|
* |
55
|
|
|
* @param string $serverUri Uri to a seq server instance. |
56
|
|
|
* @param string|null $apiKey A Seq API key to authenticate or tag messages from the logger. |
57
|
|
|
* @param int $level The minimum logging level at which this handler will be triggered. |
58
|
|
|
* @param boolean $bubble Whether the messages that are handled can bubble up the stack or not. |
59
|
|
|
* @param HttpClient|null $client An instance of a psr-7 http client implementation or null when the |
60
|
|
|
* HttpClientDiscovery should be used to find an instance. |
61
|
|
|
* @param MessageFactory|null $factory An instance of a psr-7 message factory implementation or null when |
62
|
|
|
* the MessageFactoryDiscovery should be used to find an instance. |
63
|
|
|
*/ |
64
|
|
|
public function __construct( |
65
|
|
|
string $serverUri, |
66
|
|
|
string $apiKey = null, |
67
|
|
|
$level = Logger::DEBUG, |
68
|
|
|
$bubble = true, |
69
|
|
|
HttpClient $client = null, |
70
|
|
|
MessageFactory $factory = null |
71
|
|
|
) { |
72
|
|
|
$this->setServerUri($serverUri); |
73
|
|
|
$this->setApiKey($apiKey); |
74
|
|
|
|
75
|
|
|
parent::__construct( |
76
|
|
|
$this->options, |
77
|
|
|
$client, |
78
|
|
|
$factory, |
79
|
|
|
$level, |
80
|
|
|
$bubble |
81
|
|
|
); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Gets the seq server uri. |
86
|
|
|
* |
87
|
|
|
* @return string|null |
88
|
|
|
*/ |
89
|
|
|
public function getServerUri() |
90
|
|
|
{ |
91
|
|
|
$uri = $this->getUri(); |
92
|
|
|
|
93
|
|
|
if (!empty($uri)) { |
94
|
|
|
return str_replace(SeqHandler::SEQ_API_URI, '', $uri); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return null; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Sets the seq server uri. |
102
|
|
|
* |
103
|
|
|
* @param string|null $uri Uri to the seq server instance e.g. 'http://seq-server' or null to disable the |
104
|
|
|
* {@see SeqHandler}. |
105
|
|
|
* @return self |
106
|
|
|
*/ |
107
|
|
|
public function setServerUri(string $uri = null) |
108
|
|
|
{ |
109
|
|
|
if (!empty($uri)) { |
110
|
|
|
if (!SeqHandler::endsWith($uri, '/')) { |
111
|
|
|
$uri = $uri . '/'; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$uri = $uri . SeqHandler::SEQ_API_URI; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
$this->setUri($uri); |
118
|
|
|
|
119
|
|
|
return $this; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Gets the Seq API key. |
124
|
|
|
* |
125
|
|
|
* @return string|null |
126
|
|
|
*/ |
127
|
|
|
public function getApiKey() |
128
|
|
|
{ |
129
|
|
|
return $this->getHeader('X-Seq-ApiKey'); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Sets the Seq API key to authenticate or tag messages from the logger. |
134
|
|
|
* |
135
|
|
|
* @param string|null $apiKey The Seq API key or null. |
136
|
|
|
* @return self |
137
|
|
|
*/ |
138
|
|
|
public function setApiKey(string $apiKey = null) |
139
|
|
|
{ |
140
|
|
|
$this->popHeader($apiKey); |
141
|
|
|
|
142
|
|
|
if (!empty($apiKey)) { |
143
|
|
|
$this->pushHeader('X-Seq-ApiKey', $apiKey); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
return $this; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Sets the formatter. |
151
|
|
|
* |
152
|
|
|
* @param FormatterInterface $formatter The formatter of type SeqBaseFormatter. |
153
|
|
|
* @return self |
154
|
|
|
*/ |
155
|
|
|
public function setFormatter(FormatterInterface $formatter) |
156
|
|
|
{ |
157
|
|
|
if (!($formatter instanceof SeqBaseFormatter)) { |
158
|
|
|
throw new \InvalidArgumentException('SeqBaseFormatter expected, got ' . gettype($formatter) . ' / ' . get_class($formatter)); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
$this->formatter = $formatter; |
162
|
|
|
|
163
|
|
|
$this->pushHeader('Content-Type', $formatter->getContentType()); |
164
|
|
|
|
165
|
|
|
return $this; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Gets the default formatter. |
170
|
|
|
* |
171
|
|
|
* @return \Msschl\Monolog\Formatter\SeqCompactJsonFormatter |
172
|
|
|
*/ |
173
|
|
|
protected function getDefaultFormatter() : FormatterInterface |
174
|
|
|
{ |
175
|
|
|
$formatter = new SeqCompactJsonFormatter(); |
176
|
|
|
|
177
|
|
|
$this->pushHeader('Content-Type', $formatter->getContentType()); |
178
|
|
|
|
179
|
|
|
return $formatter; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Checks whether a string ends with a specific string or not. |
184
|
|
|
* |
185
|
|
|
* @param string $haystack The string to check. |
186
|
|
|
* @param string|null $needle The searched value. |
187
|
|
|
* @return bool |
188
|
|
|
*/ |
189
|
|
|
private static function endsWith(string $haystack, string $needle = null) |
190
|
|
|
{ |
191
|
|
|
$length = strlen($needle); |
192
|
|
|
|
193
|
|
|
return $length === 0 || (substr($haystack, -$length) === $needle); |
194
|
|
|
} |
195
|
|
|
} |