1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Msschl\Monolog\Formatter; |
4
|
|
|
|
5
|
|
|
use DateTime; |
6
|
|
|
use Monolog\Formatter\FormatterInterface; |
7
|
|
|
use Monolog\Formatter\JsonFormatter; |
8
|
|
|
use Msschl\Monolog\Exception\InvalidCodePathException; |
9
|
|
|
use Throwable; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* This file is part of the msschl\monolog-seq-handler package. |
13
|
|
|
* |
14
|
|
|
* Copyright (c) 2018 Markus Schlotbohm |
15
|
|
|
* |
16
|
|
|
* For the full copyright and license information, please view the LICENSE.md |
17
|
|
|
* file that was distributed with this source code. |
18
|
|
|
*/ |
19
|
|
|
class SeqCompactJsonFormatter extends SeqBaseFormatter |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The extract context flag. |
24
|
|
|
* Whether to extract the context array to the root or not. |
25
|
|
|
* |
26
|
|
|
* @var bool |
27
|
|
|
*/ |
28
|
|
|
protected $extractContext; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* The extract extras flag. |
32
|
|
|
* Whether to extract the extras array to the root or not. |
33
|
|
|
* |
34
|
|
|
* @var bool |
35
|
|
|
*/ |
36
|
|
|
protected $extractExtras; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Initializes a new instance of the {@see SeqCompactJsonFormatter} class. |
40
|
|
|
* |
41
|
|
|
* @param bool $extractContext Flag that indicates whether to extract the extras array |
42
|
|
|
* to the root or not. |
43
|
|
|
* @param bool $extractExtras Flag that indicates whether to extract the context array |
44
|
|
|
* to the root or not. |
45
|
|
|
*/ |
46
|
|
|
public function __construct(bool $extractContext = true, bool $extractExtras = true) |
47
|
|
|
{ |
48
|
|
|
$this->extractContext = $extractContext; |
49
|
|
|
$this->extractExtras = $extractExtras; |
50
|
|
|
|
51
|
|
|
parent::__construct(JsonFormatter::BATCH_MODE_NEWLINES); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Returns a string with the content type for the seq-formatter. |
56
|
|
|
* |
57
|
|
|
* @return string |
58
|
|
|
*/ |
59
|
|
|
public function getContentType() : string { |
60
|
|
|
return 'application/vnd.serilog.clef'; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Gets whether the flag extract content is set or not. |
65
|
|
|
* |
66
|
|
|
* @return bool |
67
|
|
|
*/ |
68
|
|
|
public function getExtractContent() : bool |
69
|
|
|
{ |
70
|
|
|
return $this->extractContext; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Sets the flag extract content. |
75
|
|
|
* |
76
|
|
|
* @param bool $value The flag. |
77
|
|
|
* @return self |
78
|
|
|
*/ |
79
|
|
|
public function setExtractContent(bool $value) |
80
|
|
|
{ |
81
|
|
|
$this->extractContext = $value; |
82
|
|
|
|
83
|
|
|
return $this; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Gets whether the flag extract extras is set or not. |
88
|
|
|
* |
89
|
|
|
* @return bool |
90
|
|
|
*/ |
91
|
|
|
public function getExtractExtras() |
92
|
|
|
{ |
93
|
|
|
return $this->extractExtras; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Sets the flag extract extras. |
98
|
|
|
* |
99
|
|
|
* @param bool $value The flag. |
100
|
|
|
* @return self |
101
|
|
|
*/ |
102
|
|
|
public function setExtractExtras(bool $value) |
103
|
|
|
{ |
104
|
|
|
$this->extractExtras = $value; |
105
|
|
|
|
106
|
|
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Return a JSON-encoded array of records. |
111
|
|
|
* |
112
|
|
|
* @param array $records |
113
|
|
|
* @return string |
114
|
|
|
*/ |
115
|
|
|
protected function formatBatchJson(array $records) |
116
|
|
|
{ |
117
|
|
|
throw new InvalidCodePathException(); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Processes the log message. |
122
|
|
|
* |
123
|
|
|
* @param array &$normalized Reference to the normalized array, where all normalized data get stored. |
124
|
|
|
* @param string $message The log message. |
125
|
|
|
* @return void |
126
|
|
|
*/ |
127
|
|
|
protected function processMessage(array &$normalized, string $message) |
128
|
|
|
{ |
129
|
|
|
$normalized['@m'] = $message; |
130
|
|
|
if (!(strpos($message, '{') === false)) { |
131
|
|
|
$normalized['@mt'] = $message; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Processes the context array. |
137
|
|
|
* |
138
|
|
|
* @param array &$normalized Reference to the normalized array, where all normalized data get stored. |
139
|
|
|
* @param array $message The context array. |
140
|
|
|
* @return void |
141
|
|
|
*/ |
142
|
|
|
protected function processContext(array &$normalized, array $context) |
143
|
|
|
{ |
144
|
|
|
$this->processContextException($normalized, $context); |
145
|
|
|
$array = $this->getNormalizedArray($context); |
146
|
|
|
|
147
|
|
|
if ($this->extractContext) { |
148
|
|
|
$normalized = array_merge($array, $normalized); |
149
|
|
|
} else { |
150
|
|
|
$normalized['Context'] = $array; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Processes the log level. |
156
|
|
|
* |
157
|
|
|
* @param array &$normalized Reference to the normalized array, where all normalized data get stored. |
158
|
|
|
* @param int $message The log level. |
159
|
|
|
* @return void |
160
|
|
|
*/ |
161
|
|
|
protected function processLevel(array &$normalized, int $level) |
162
|
|
|
{ |
163
|
|
|
$normalized['@l'] = $this->logLevelMap[$level]; |
164
|
|
|
$normalized['Code'] = $level; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Processes the log level name. |
169
|
|
|
* |
170
|
|
|
* @param array &$normalized Reference to the normalized array, where all normalized data get stored. |
171
|
|
|
* @param string $message The log level name. |
172
|
|
|
* @return void |
173
|
|
|
*/ |
174
|
|
|
protected function processLevelName(array &$normalized, string $levelName) |
175
|
|
|
{ |
176
|
|
|
$normalized['LevelName'] = $levelName; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Processes the channel name. |
181
|
|
|
* |
182
|
|
|
* @param array &$normalized Reference to the normalized array, where all normalized data get stored. |
183
|
|
|
* @param string $message The log channel name. |
184
|
|
|
* @return void |
185
|
|
|
*/ |
186
|
|
|
protected function processChannel(array &$normalized, string $name) |
187
|
|
|
{ |
188
|
|
|
$normalized['Channel'] = $name; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Processes the log timestamp. |
193
|
|
|
* |
194
|
|
|
* @param array &$normalized Reference to the normalized array, where all normalized data get stored. |
195
|
|
|
* @param DateTime $message The log timestamp. |
196
|
|
|
* @return void |
197
|
|
|
*/ |
198
|
|
|
protected function processDatetime(array &$normalized, \DateTime $datetime) |
199
|
|
|
{ |
200
|
|
|
$normalized['@t'] = $datetime->format(DateTime::ISO8601); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Processes the extras array. |
205
|
|
|
* |
206
|
|
|
* @param array &$normalized Reference to the normalized array, where all normalized data get stored. |
207
|
|
|
* @param array $message The extras array. |
208
|
|
|
* @return void |
209
|
|
|
*/ |
210
|
|
|
protected function processExtra(array &$normalized, array $extras) |
211
|
|
|
{ |
212
|
|
|
$array = $this->getNormalizedArray($extras); |
213
|
|
|
|
214
|
|
|
if ($this->extractExtras) { |
215
|
|
|
$normalized = array_merge($array, $normalized); |
216
|
|
|
} else { |
217
|
|
|
$normalized['Extra'] = $array; |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Extracts the exception from the context array. |
223
|
|
|
* |
224
|
|
|
* @param array &$normalized Reference to the normalized array, where all normalized data get stored. |
225
|
|
|
* @param array $context The context array. |
226
|
|
|
* @return void |
227
|
|
|
*/ |
228
|
|
|
private function processContextException(array &$normalized, array $context) |
229
|
|
|
{ |
230
|
|
|
$exception = $this->extractException($context); |
231
|
|
|
if ($exception !== null) { |
232
|
|
|
$normalized['@x'] = $this->normalizeException($exception); |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Gets a normalized array. |
238
|
|
|
* |
239
|
|
|
* @param array $array The array to process. |
240
|
|
|
* @return array |
241
|
|
|
*/ |
242
|
|
|
private function getNormalizedArray(array $array) : array |
243
|
|
|
{ |
244
|
|
|
$normalized = []; |
245
|
|
|
$count = 1; |
246
|
|
|
foreach ($array as $key => $value) { |
247
|
|
|
if ($count++ >= 1000) { |
248
|
|
|
$normalized['...'] = 'Over 1000 items, aborting normalization'; |
249
|
|
|
break; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
if (is_int($key)) { |
253
|
|
|
$normalized[] = $value; |
254
|
|
|
} else { |
255
|
|
|
$key = SeqCompactJsonFormatter::ConvertSnakeCaseToPascalCase($key); |
256
|
|
|
$normalized[$key] = $value; |
257
|
|
|
} |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
return $normalized; |
261
|
|
|
} |
262
|
|
|
} |