|
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) |
|
62
|
|
|
{ |
|
63
|
|
|
if (!is_array($record) && !$record instanceof \Traversable) { |
|
64
|
|
|
throw new \InvalidArgumentException('Array/Traversable expected, got ' . gettype($record) . ' / ' . get_class($record)); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
$normalized = []; |
|
68
|
|
|
|
|
69
|
|
|
foreach ($record as $key => $value) { |
|
70
|
|
|
$key = SeqBaseFormatter::ConvertSnakeCaseToPascalCase($key); |
|
71
|
|
|
|
|
72
|
|
|
$this->{'process' . $key}($normalized, $value); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return $normalized; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Processes the log message. |
|
80
|
|
|
* |
|
81
|
|
|
* @param array &$normalized Reference to the normalized array, where all normalized data get stored. |
|
82
|
|
|
* @param string $message The log message. |
|
83
|
|
|
* @return void |
|
84
|
|
|
*/ |
|
85
|
|
|
protected abstract function processMessage(array &$normalized, string $message); |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Processes the context array. |
|
89
|
|
|
* |
|
90
|
|
|
* @param array &$normalized Reference to the normalized array, where all normalized data get stored. |
|
91
|
|
|
* @param array $message The context array. |
|
92
|
|
|
* @return void |
|
93
|
|
|
*/ |
|
94
|
|
|
protected abstract function processContext(array &$normalized, array $context); |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Processes the log level. |
|
98
|
|
|
* |
|
99
|
|
|
* @param array &$normalized Reference to the normalized array, where all normalized data get stored. |
|
100
|
|
|
* @param int $message The log level. |
|
101
|
|
|
* @return void |
|
102
|
|
|
*/ |
|
103
|
|
|
protected abstract function processLevel(array &$normalized, int $level); |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Processes the log level name. |
|
107
|
|
|
* |
|
108
|
|
|
* @param array &$normalized Reference to the normalized array, where all normalized data get stored. |
|
109
|
|
|
* @param string $message The log level name. |
|
110
|
|
|
* @return void |
|
111
|
|
|
*/ |
|
112
|
|
|
protected abstract function processLevelName(array &$normalized, string $levelName); |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Processes the channel name. |
|
116
|
|
|
* |
|
117
|
|
|
* @param array &$normalized Reference to the normalized array, where all normalized data get stored. |
|
118
|
|
|
* @param string $message The log channel name. |
|
119
|
|
|
* @return void |
|
120
|
|
|
*/ |
|
121
|
|
|
protected abstract function processChannel(array &$normalized, string $name); |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Processes the log timestamp. |
|
125
|
|
|
* |
|
126
|
|
|
* @param array &$normalized Reference to the normalized array, where all normalized data get stored. |
|
127
|
|
|
* @param DateTime $message The log timestamp. |
|
128
|
|
|
* @return void |
|
129
|
|
|
*/ |
|
130
|
|
|
protected abstract function processDatetime(array &$normalized, DateTime $datetime); |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Processes the extras array. |
|
134
|
|
|
* |
|
135
|
|
|
* @param array &$normalized Reference to the normalized array, where all normalized data get stored. |
|
136
|
|
|
* @param array $message The extras array. |
|
137
|
|
|
* @return void |
|
138
|
|
|
*/ |
|
139
|
|
|
protected abstract function processExtra(array &$normalized, array $extras); |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Normalizes an exception to a string. |
|
143
|
|
|
* |
|
144
|
|
|
* @param Throwable $e The throwable instance to normalize. |
|
|
|
|
|
|
145
|
|
|
* @return string |
|
146
|
|
|
*/ |
|
147
|
|
|
protected function normalizeException($e) : string |
|
148
|
|
|
{ |
|
149
|
|
|
$previousText = ''; |
|
150
|
|
|
if ($previous = $e->getPrevious()) { |
|
151
|
|
|
do { |
|
152
|
|
|
$previousText .= ', ' . get_class($previous) . '(code: ' . $previous->getCode() . '): ' . $previous->getMessage() . ' at ' . $previous->getFile() . ':' . $previous->getLine(); |
|
153
|
|
|
} while ($previous = $previous->getPrevious()); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
$str = '[object] (' . get_class($e) . '(code: ' . $e->getCode() . '): ' . $e->getMessage() . ' at ' . $e->getFile() . ':' . $e->getLine() . $previousText . ')'; |
|
157
|
|
|
if ($this->includeStacktraces) { |
|
158
|
|
|
$str .= "\n[stacktrace]\n" . $e->getTraceAsString() . "\n"; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
return $str; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Extracts the exception from an array. |
|
166
|
|
|
* |
|
167
|
|
|
* @param array &$array The array. |
|
168
|
|
|
* @return \Throwable|null |
|
169
|
|
|
*/ |
|
170
|
|
|
protected function extractException(array &$array) { |
|
171
|
|
|
$exception = $array['exception'] ?? null; |
|
172
|
|
|
|
|
173
|
|
|
if ($exception === null) { |
|
174
|
|
|
return null; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
unset($array['exception']); |
|
178
|
|
|
|
|
179
|
|
|
if (!($exception instanceof \Throwable)) { |
|
180
|
|
|
return null; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
return $exception; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* Converts a snake case string to a pascal case string. |
|
188
|
|
|
* |
|
189
|
|
|
* @param string $value The string to convert. |
|
190
|
|
|
* @return string |
|
191
|
|
|
*/ |
|
192
|
|
|
protected static function ConvertSnakeCaseToPascalCase(string $value = null) : string { |
|
193
|
|
|
return str_replace(' ', '', ucwords(str_replace(['-', '_'], ' ', $value))); |
|
194
|
|
|
} |
|
195
|
|
|
} |