1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Msschl\Monolog\Formatter; |
4
|
|
|
|
5
|
|
|
use DateTime; |
6
|
|
|
use Monolog\Formatter\FormatterInterface; |
7
|
|
|
use Monolog\Formatter\JsonFormatter; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* This file is part of the msschl\monolog-seq-handler package. |
11
|
|
|
* |
12
|
|
|
* Copyright (c) 2018 Markus Schlotbohm |
13
|
|
|
* |
14
|
|
|
* For the full copyright and license information, please view the LICENSE.md |
15
|
|
|
* file that was distributed with this source code. |
16
|
|
|
*/ |
17
|
|
|
abstract class SeqBaseFormatter extends JsonFormatter |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* The log level map. |
22
|
|
|
* Maps the monolog log levels to the seq log levels. |
23
|
|
|
* |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
protected $logLevelMap = [ |
27
|
|
|
'100' => 'Debug', |
28
|
|
|
'200' => 'Information', |
29
|
|
|
'250' => 'Information', |
30
|
|
|
'300' => 'Warning', |
31
|
|
|
'400' => 'Error', |
32
|
|
|
'500' => 'Error', |
33
|
|
|
'550' => 'Fatal', |
34
|
|
|
'600' => 'Fatal', |
35
|
|
|
]; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Initializes a new instance of the {@see SeqBaseFormatter} class. |
39
|
|
|
* |
40
|
|
|
* @param int $batchMode The json batch mode. |
41
|
|
|
*/ |
42
|
|
|
function __construct($batchMode) |
43
|
|
|
{ |
44
|
|
|
$this->appendNewline = false; |
45
|
|
|
$this->batchMode = $batchMode; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Returns a string with the content type for the seq-formatter. |
50
|
|
|
* |
51
|
|
|
* @return string |
52
|
|
|
*/ |
53
|
|
|
public abstract function getContentType() : string; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Normalizes the log record array. |
57
|
|
|
* |
58
|
|
|
* @param array $recod The log record to normalize. |
59
|
|
|
* @return array |
60
|
|
|
*/ |
61
|
|
|
protected function normalize($record, $depth = 0) |
62
|
|
|
{ |
63
|
|
|
if (!is_array($record) && !$record instanceof \Traversable) { |
64
|
|
|
/* istanbul ignore next */ |
65
|
|
|
throw new \InvalidArgumentException('Array/Traversable expected, got ' . gettype($record) . ' / ' . get_class($record)); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$normalized = []; |
69
|
|
|
|
70
|
|
|
foreach ($record as $key => $value) { |
71
|
|
|
$key = SeqBaseFormatter::ConvertSnakeCaseToPascalCase($key); |
72
|
|
|
|
73
|
|
|
$this->{'process' . $key}($normalized, $value); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $normalized; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Processes the log message. |
81
|
|
|
* |
82
|
|
|
* @param array &$normalized Reference to the normalized array, where all normalized data get stored. |
83
|
|
|
* @param string $message The log message. |
84
|
|
|
* @return void |
85
|
|
|
*/ |
86
|
|
|
protected abstract function processMessage(array &$normalized, string $message); |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Processes the context array. |
90
|
|
|
* |
91
|
|
|
* @param array &$normalized Reference to the normalized array, where all normalized data get stored. |
92
|
|
|
* @param array $message The context array. |
93
|
|
|
* @return void |
94
|
|
|
*/ |
95
|
|
|
protected abstract function processContext(array &$normalized, array $context); |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Processes the log level. |
99
|
|
|
* |
100
|
|
|
* @param array &$normalized Reference to the normalized array, where all normalized data get stored. |
101
|
|
|
* @param int $message The log level. |
102
|
|
|
* @return void |
103
|
|
|
*/ |
104
|
|
|
protected abstract function processLevel(array &$normalized, int $level); |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Processes the log level name. |
108
|
|
|
* |
109
|
|
|
* @param array &$normalized Reference to the normalized array, where all normalized data get stored. |
110
|
|
|
* @param string $message The log level name. |
111
|
|
|
* @return void |
112
|
|
|
*/ |
113
|
|
|
protected abstract function processLevelName(array &$normalized, string $levelName); |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Processes the channel name. |
117
|
|
|
* |
118
|
|
|
* @param array &$normalized Reference to the normalized array, where all normalized data get stored. |
119
|
|
|
* @param string $message The log channel name. |
120
|
|
|
* @return void |
121
|
|
|
*/ |
122
|
|
|
protected abstract function processChannel(array &$normalized, string $name); |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Processes the log timestamp. |
126
|
|
|
* |
127
|
|
|
* @param array &$normalized Reference to the normalized array, where all normalized data get stored. |
128
|
|
|
* @param DateTime $message The log timestamp. |
129
|
|
|
* @return void |
130
|
|
|
*/ |
131
|
|
|
protected abstract function processDatetime(array &$normalized, DateTime $datetime); |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Processes the extras array. |
135
|
|
|
* |
136
|
|
|
* @param array &$normalized Reference to the normalized array, where all normalized data get stored. |
137
|
|
|
* @param array $message The extras array. |
138
|
|
|
* @return void |
139
|
|
|
*/ |
140
|
|
|
protected abstract function processExtra(array &$normalized, array $extras); |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Normalizes an exception to a string. |
144
|
|
|
* |
145
|
|
|
* @param Throwable $e The throwable instance to normalize. |
|
|
|
|
146
|
|
|
* @return string |
147
|
|
|
*/ |
148
|
|
|
protected function normalizeException($e) : string |
149
|
|
|
{ |
150
|
|
|
$previousText = ''; |
151
|
|
|
if ($previous = $e->getPrevious()) { |
152
|
|
|
do { |
153
|
|
|
$previousText .= ', ' . get_class($previous) . '(code: ' . $previous->getCode() . '): ' . $previous->getMessage() . ' at ' . $previous->getFile() . ':' . $previous->getLine(); |
154
|
|
|
} while ($previous = $previous->getPrevious()); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
$str = '[object] (' . get_class($e) . '(code: ' . $e->getCode() . '): ' . $e->getMessage() . ' at ' . $e->getFile() . ':' . $e->getLine() . $previousText . ')'; |
158
|
|
|
if ($this->includeStacktraces) { |
159
|
|
|
$str .= "\n[stacktrace]\n" . $e->getTraceAsString() . "\n"; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
return $str; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Extracts the exception from an array. |
167
|
|
|
* |
168
|
|
|
* @param array &$array The array. |
169
|
|
|
* @return \Throwable|null |
170
|
|
|
*/ |
171
|
|
|
protected function extractException(array &$array) { |
172
|
|
|
$exception = $array['exception'] ?? null; |
173
|
|
|
|
174
|
|
|
if ($exception === null) { |
175
|
|
|
return null; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
unset($array['exception']); |
179
|
|
|
|
180
|
|
|
if (!($exception instanceof \Throwable)) { |
181
|
|
|
return null; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
return $exception; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Converts a snake case string to a pascal case string. |
189
|
|
|
* |
190
|
|
|
* @param string $value The string to convert. |
191
|
|
|
* @return string |
192
|
|
|
*/ |
193
|
|
|
protected static function ConvertSnakeCaseToPascalCase(string $value = null) : string { |
194
|
|
|
return str_replace(' ', '', ucwords(str_replace(['-', '_'], ' ', $value))); |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|