|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the php-gelf package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Benjamin Zikarsky <http://benjamin-zikarsky.de> |
|
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 Gelf; |
|
13
|
|
|
|
|
14
|
|
|
use Psr\Log\LogLevel; |
|
15
|
|
|
use RuntimeException; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* A message complying to the GELF standard |
|
19
|
|
|
* <https://github.com/Graylog2/graylog2-docs/wiki/GELF> |
|
20
|
|
|
* |
|
21
|
|
|
* @author Benjamin Zikarsky <[email protected]> |
|
22
|
|
|
*/ |
|
23
|
|
|
class Message implements MessageInterface |
|
24
|
|
|
{ |
|
25
|
|
|
|
|
26
|
|
|
protected $host; |
|
27
|
|
|
protected $shortMessage; |
|
28
|
|
|
protected $fullMessage; |
|
29
|
|
|
protected $timestamp; |
|
30
|
|
|
protected $level; |
|
31
|
|
|
protected $facility; |
|
32
|
|
|
protected $file; |
|
33
|
|
|
protected $line; |
|
34
|
|
|
protected $additionals = array(); |
|
35
|
|
|
protected $version; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* A list of the PSR LogLevel constants which is also a mapping of |
|
39
|
|
|
* syslog code to psr-value |
|
40
|
|
|
* |
|
41
|
|
|
* @var array |
|
42
|
|
|
*/ |
|
43
|
|
|
private static $psrLevels = array( |
|
44
|
|
|
LogLevel::EMERGENCY, // 0 |
|
45
|
|
|
LogLevel::ALERT, // 1 |
|
46
|
|
|
LogLevel::CRITICAL, // 2 |
|
47
|
|
|
LogLevel::ERROR, // 3 |
|
48
|
|
|
LogLevel::WARNING, // 4 |
|
49
|
|
|
LogLevel::NOTICE, // 5 |
|
50
|
|
|
LogLevel::INFO, // 6 |
|
51
|
|
|
LogLevel::DEBUG // 7 |
|
52
|
|
|
); |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Creates a new message |
|
56
|
|
|
* |
|
57
|
|
|
* Populates timestamp and host with sane default values |
|
58
|
|
|
*/ |
|
59
|
63 |
|
public function __construct() |
|
60
|
|
|
{ |
|
61
|
63 |
|
$this->timestamp = microtime(true); |
|
62
|
63 |
|
$this->host = gethostname(); |
|
63
|
63 |
|
$this->level = 1; //ALERT |
|
64
|
63 |
|
$this->version = "1.0"; |
|
65
|
63 |
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Trys to convert a given log-level (psr or syslog) to |
|
69
|
|
|
* the psr representation |
|
70
|
|
|
* |
|
71
|
|
|
* @param mixed $level |
|
72
|
|
|
* @return string |
|
73
|
|
|
*/ |
|
74
|
5 |
|
final public static function logLevelToPsr($level) |
|
75
|
|
|
{ |
|
76
|
5 |
|
$origLevel = $level; |
|
77
|
|
|
|
|
78
|
5 |
|
if (is_numeric($level)) { |
|
79
|
4 |
|
$level = intval($level); |
|
80
|
4 |
|
if (isset(self::$psrLevels[$level])) { |
|
81
|
4 |
|
return self::$psrLevels[$level]; |
|
82
|
|
|
} |
|
83
|
2 |
|
} elseif (is_string($level)) { |
|
84
|
2 |
|
$level = strtolower($level); |
|
85
|
2 |
|
if (in_array($level, self::$psrLevels)) { |
|
86
|
1 |
|
return $level; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
2 |
|
throw new RuntimeException( |
|
91
|
2 |
|
sprintf("Cannot convert log-level '%s' to psr-style", $origLevel) |
|
92
|
|
|
); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Trys to convert a given log-level (psr or syslog) to |
|
97
|
|
|
* the syslog representation |
|
98
|
|
|
* |
|
99
|
|
|
* @param mxied |
|
100
|
|
|
* @return integer |
|
|
|
|
|
|
101
|
|
|
*/ |
|
102
|
21 |
|
final public static function logLevelToSyslog($level) |
|
103
|
|
|
{ |
|
104
|
21 |
|
$origLevel = $level; |
|
105
|
|
|
|
|
106
|
21 |
|
if (is_numeric($level)) { |
|
107
|
14 |
|
$level = intval($level); |
|
108
|
14 |
|
if ($level < 8 && $level > -1) { |
|
109
|
14 |
|
return $level; |
|
110
|
|
|
} |
|
111
|
17 |
|
} elseif (is_string($level)) { |
|
112
|
17 |
|
$level = strtolower($level); |
|
113
|
17 |
|
$syslogLevel = array_search($level, self::$psrLevels); |
|
114
|
17 |
|
if (false !== $syslogLevel) { |
|
115
|
16 |
|
return $syslogLevel; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
2 |
|
throw new RuntimeException( |
|
120
|
2 |
|
sprintf("Cannot convert log-level '%s' to syslog-style", $origLevel) |
|
121
|
|
|
); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
13 |
|
public function getVersion() |
|
125
|
|
|
{ |
|
126
|
13 |
|
return $this->version; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
3 |
|
public function setVersion($version) |
|
130
|
|
|
{ |
|
131
|
3 |
|
$this->version = $version; |
|
132
|
|
|
|
|
133
|
3 |
|
return $this; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
13 |
|
public function getHost() |
|
137
|
|
|
{ |
|
138
|
13 |
|
return $this->host; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
2 |
|
public function setHost($host) |
|
142
|
|
|
{ |
|
143
|
2 |
|
$this->host = $host; |
|
144
|
|
|
|
|
145
|
2 |
|
return $this; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
17 |
|
public function getShortMessage() |
|
149
|
|
|
{ |
|
150
|
17 |
|
return $this->shortMessage; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
17 |
|
public function setShortMessage($shortMessage) |
|
154
|
|
|
{ |
|
155
|
17 |
|
$this->shortMessage = $shortMessage; |
|
156
|
|
|
|
|
157
|
17 |
|
return $this; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
14 |
|
public function getFullMessage() |
|
161
|
|
|
{ |
|
162
|
14 |
|
return $this->fullMessage; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
3 |
|
public function setFullMessage($fullMessage) |
|
166
|
|
|
{ |
|
167
|
3 |
|
$this->fullMessage = $fullMessage; |
|
168
|
|
|
|
|
169
|
3 |
|
return $this; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
14 |
|
public function getTimestamp() |
|
173
|
|
|
{ |
|
174
|
14 |
|
return (float) $this->timestamp; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
3 |
|
public function setTimestamp($timestamp) |
|
178
|
|
|
{ |
|
179
|
3 |
|
if ($timestamp instanceof \DateTime || $timestamp instanceof \DateTimeInterface) { |
|
180
|
2 |
|
$timestamp = $timestamp->format("U.u"); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
3 |
|
$this->timestamp = (float) $timestamp; |
|
184
|
|
|
|
|
185
|
3 |
|
return $this; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
2 |
|
public function getLevel() |
|
189
|
|
|
{ |
|
190
|
2 |
|
return self::logLevelToPsr($this->level); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
13 |
|
public function getSyslogLevel() |
|
194
|
|
|
{ |
|
195
|
13 |
|
return self::logLevelToSyslog($this->level); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
18 |
|
public function setLevel($level) |
|
199
|
|
|
{ |
|
200
|
18 |
|
$this->level = self::logLevelToSyslog($level); |
|
201
|
|
|
|
|
202
|
16 |
|
return $this; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
14 |
|
public function getFacility() |
|
206
|
|
|
{ |
|
207
|
14 |
|
return $this->facility; |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
16 |
|
public function setFacility($facility) |
|
211
|
|
|
{ |
|
212
|
16 |
|
$this->facility = $facility; |
|
213
|
|
|
|
|
214
|
16 |
|
return $this; |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
14 |
|
public function getFile() |
|
218
|
|
|
{ |
|
219
|
14 |
|
return $this->file; |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
4 |
|
public function setFile($file) |
|
223
|
|
|
{ |
|
224
|
4 |
|
$this->file = $file; |
|
225
|
|
|
|
|
226
|
4 |
|
return $this; |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
14 |
|
public function getLine() |
|
230
|
|
|
{ |
|
231
|
14 |
|
return $this->line; |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
4 |
|
public function setLine($line) |
|
235
|
|
|
{ |
|
236
|
4 |
|
$this->line = $line; |
|
237
|
|
|
|
|
238
|
4 |
|
return $this; |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
2 |
|
public function getAdditional($key) |
|
242
|
|
|
{ |
|
243
|
2 |
|
if (!isset($this->additionals[$key])) { |
|
244
|
1 |
|
throw new RuntimeException( |
|
245
|
1 |
|
sprintf("Additional key '%s' is not defined", $key) |
|
246
|
|
|
); |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
1 |
|
return $this->additionals[$key]; |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
1 |
|
public function hasAdditional($key) |
|
253
|
|
|
{ |
|
254
|
1 |
|
return isset($this->additionals[$key]); |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
17 |
|
public function setAdditional($key, $value) |
|
258
|
|
|
{ |
|
259
|
17 |
|
if (!$key) { |
|
260
|
1 |
|
throw new RuntimeException("Additional field key cannot be empty"); |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
16 |
|
$this->additionals[$key] = $value; |
|
264
|
|
|
|
|
265
|
16 |
|
return $this; |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
14 |
|
public function getAllAdditionals() |
|
269
|
|
|
{ |
|
270
|
14 |
|
return $this->additionals; |
|
271
|
|
|
} |
|
272
|
|
|
|
|
273
|
12 |
|
public function toArray() |
|
274
|
|
|
{ |
|
275
|
|
|
$message = array( |
|
276
|
12 |
|
'version' => $this->getVersion(), |
|
277
|
12 |
|
'host' => $this->getHost(), |
|
278
|
12 |
|
'short_message' => $this->getShortMessage(), |
|
279
|
12 |
|
'full_message' => $this->getFullMessage(), |
|
280
|
12 |
|
'level' => $this->getSyslogLevel(), |
|
281
|
12 |
|
'timestamp' => $this->getTimestamp(), |
|
282
|
12 |
|
'facility' => $this->getFacility(), |
|
283
|
12 |
|
'file' => $this->getFile(), |
|
284
|
12 |
|
'line' => $this->getLine() |
|
285
|
|
|
); |
|
286
|
|
|
|
|
287
|
|
|
// Transform 1.1 deprecated fields to additionals |
|
288
|
|
|
// Will be refactored for 2.0, see #23 |
|
289
|
12 |
|
if ($this->getVersion() == "1.1") { |
|
290
|
1 |
|
foreach (array('line', 'facility', 'file') as $idx) { |
|
291
|
1 |
|
$message["_" . $idx] = $message[$idx]; |
|
292
|
1 |
|
unset($message[$idx]); |
|
293
|
|
|
} |
|
294
|
|
|
} |
|
295
|
|
|
|
|
296
|
|
|
// add additionals |
|
297
|
12 |
|
foreach ($this->getAllAdditionals() as $key => $value) { |
|
298
|
12 |
|
$message["_" . $key] = $value; |
|
299
|
|
|
} |
|
300
|
|
|
|
|
301
|
|
|
// return after filtering empty strings and null values |
|
302
|
12 |
|
return array_filter($message, function ($message) { |
|
303
|
12 |
|
return is_bool($message) |
|
304
|
12 |
|
|| (is_string($message) && strlen($message)) |
|
305
|
12 |
|
|| !empty($message); |
|
306
|
12 |
|
}); |
|
307
|
|
|
} |
|
308
|
|
|
} |
|
309
|
|
|
|
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.