|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* src/Logs/Formatter.php. |
|
4
|
|
|
* |
|
5
|
|
|
* @author Austin Heap <[email protected]> |
|
6
|
|
|
* @version v0.1.6 |
|
7
|
|
|
*/ |
|
8
|
|
|
declare(strict_types = 1); |
|
9
|
|
|
|
|
10
|
|
|
namespace AustinHeap\Database\InfluxDb\Logs; |
|
11
|
|
|
|
|
12
|
|
|
use Monolog\Formatter\NormalizerFormatter; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class Formatter. |
|
16
|
|
|
*/ |
|
17
|
|
|
class Formatter extends NormalizerFormatter |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* {@inheritdoc} |
|
21
|
|
|
*/ |
|
22
|
|
|
public function format(array $record) |
|
23
|
|
|
{ |
|
24
|
|
|
$record = parent::format($record); |
|
25
|
|
|
|
|
26
|
|
|
return $this->prepareMessage($record); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param array $record |
|
31
|
|
|
* |
|
32
|
|
|
* @return array |
|
33
|
|
|
*/ |
|
34
|
|
|
protected function prepareTags(array $record): array |
|
35
|
|
|
{ |
|
36
|
|
|
$tags = []; |
|
37
|
|
|
|
|
38
|
|
|
if (isset($_SERVER['REMOTE_ADDR'])) { |
|
39
|
|
|
$tags['serverName'] = $_SERVER['REMOTE_ADDR']; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
if (isset($record['level'])) { |
|
43
|
|
|
$tags['severity'] = $this->rfc5424ToSeverity($record['level']); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
if (isset($_SERVER['REQUEST_URI'])) { |
|
47
|
|
|
$tags['endpoint_url'] = $_SERVER['REQUEST_URI']; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
if (isset($_SERVER['REQUEST_METHOD'])) { |
|
51
|
|
|
$tags['method'] = $_SERVER['REQUEST_METHOD']; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
if (isset($record['context']['user_id'])) { |
|
55
|
|
|
$tags['user_id'] = $record['context']['user_id']; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
if (isset($record['context']['project_id'])) { |
|
59
|
|
|
$tags['project_id'] = $record['context']['project_id']; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
if (isset($record['context']['file'])) { |
|
63
|
|
|
$tags['file'] = $this->replaceDigitData($record['context']['file']); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
if (isset($record['context']['event']['api_stats'][0])) { |
|
67
|
|
|
foreach ($record['context']['event']['api_stats'][0] as $key => $value) { |
|
68
|
|
|
if (is_string($value) || is_int($value)) { |
|
69
|
|
|
$tags[$key] = $value; |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return $tags; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @param array $record |
|
79
|
|
|
* |
|
80
|
|
|
* @return array |
|
81
|
|
|
*/ |
|
82
|
|
|
protected function prepareMessage(array $record): array |
|
83
|
|
|
{ |
|
84
|
|
|
$tags = $this->prepareTags($record); |
|
85
|
|
|
$message = [ |
|
86
|
|
|
'name' => 'Error', |
|
87
|
|
|
'value' => 1, |
|
88
|
|
|
'timestamp' => round(microtime(true) * 1000), |
|
89
|
|
|
]; |
|
90
|
|
|
|
|
91
|
|
|
if (count($tags)) { |
|
92
|
|
|
foreach ($tags as $key => $value) { |
|
93
|
|
|
if (is_numeric($value)) { |
|
94
|
|
|
$message['fields'][$key] = (int)$value; |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
$message['tags'] = $tags; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
if (isset($message['fields']['Debug']['message'])) { |
|
102
|
|
|
$message['fields']['Debug']['message'] = $this->trimLines($message['fields']['Debug']['message']); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
return $message; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @param int $level |
|
110
|
|
|
* |
|
111
|
|
|
* @return mixed |
|
112
|
|
|
*/ |
|
113
|
|
|
private function rfc5424ToSeverity(int $level) |
|
114
|
|
|
{ |
|
115
|
|
|
$levels = [ |
|
116
|
|
|
100 => 'Debugging', |
|
117
|
|
|
200 => 'Informational', |
|
118
|
|
|
250 => 'Notice', |
|
119
|
|
|
300 => 'Warning', |
|
120
|
|
|
400 => 'Error', |
|
121
|
|
|
500 => 'Critical', |
|
122
|
|
|
550 => 'Alert', |
|
123
|
|
|
600 => 'Emergency', |
|
124
|
|
|
]; |
|
125
|
|
|
|
|
126
|
|
|
$result = isset($levels[$level]) ? $levels[$level] : $levels[600]; |
|
127
|
|
|
|
|
128
|
|
|
return $result; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @param string $string |
|
133
|
|
|
* |
|
134
|
|
|
* @return string |
|
135
|
|
|
*/ |
|
136
|
|
|
private function replaceDigitData(string $string): string |
|
137
|
|
|
{ |
|
138
|
|
|
$string = preg_replace('~\/[0-9]+~', '/*', $string); |
|
139
|
|
|
$string = preg_replace('~\=[0-9]+~', '=*', $string); |
|
140
|
|
|
|
|
141
|
|
|
return $string; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* @param $message |
|
146
|
|
|
* |
|
147
|
|
|
* @return string |
|
148
|
|
|
*/ |
|
149
|
|
|
private function trimLines($message): string |
|
150
|
|
|
{ |
|
151
|
|
|
$limit = config('influxdb.log.limit', 5); |
|
152
|
|
|
|
|
153
|
|
|
if (is_int($limit)) { |
|
154
|
|
|
$message_array = explode(PHP_EOL, $message); |
|
155
|
|
|
|
|
156
|
|
|
if ($limit < count($message_array)) { |
|
157
|
|
|
$message = implode(PHP_EOL, array_slice($message_array, 0, $limit)); |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
return $message; |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
|