forked from getgrav/grav
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogViewer.php
More file actions
175 lines (154 loc) · 4.71 KB
/
LogViewer.php
File metadata and controls
175 lines (154 loc) · 4.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
/**
* @package Grav\Common\Helpers
*
* @copyright Copyright (c) 2015 - 2025 Trilby Media, LLC. All rights reserved.
* @license MIT License; see LICENSE file for details.
*/
namespace Grav\Common\Helpers;
use DateTime;
use DateMalformedStringException;
use function array_slice;
use function is_array;
use function is_string;
/**
* Class LogViewer
* @package Grav\Common\Helpers
*/
class LogViewer
{
/** @var string */
protected $pattern = '/\[(?P<date>.*?)\] (?P<logger>\w+)\.(?P<level>\w+): (?P<message>.*[^ ]+) (?P<context>[^ ]+) (?P<extra>[^ ]+)/';
/**
* Get the objects of a tailed file
*
* @param string $filepath
* @param int $lines
* @param bool $desc
* @return array
*/
public function objectTail($filepath, $lines = 1, $desc = true)
{
$data = $this->tail($filepath, $lines);
$tailed_log = $data ? explode(PHP_EOL, $data) : [];
$line_objects = [];
foreach ($tailed_log as $line) {
$line_objects[] = $this->parse($line);
}
return $desc ? $line_objects : array_reverse($line_objects);
}
/**
* Optimized way to get just the last few entries of a log file
*
* @param string $filepath
* @param int $lines
* @return string|false
*/
public function tail($filepath, $lines = 1)
{
$f = $filepath ? @fopen($filepath, 'rb') : false;
if ($f === false) {
return false;
}
$buffer = ($lines < 2 ? 64 : ($lines < 10 ? 512 : 4096));
fseek($f, -1, SEEK_END);
if (fread($f, 1) !== "\n") {
--$lines;
}
// Start reading
$output = '';
// While we would like more
while (ftell($f) > 0 && $lines >= 0) {
// Figure out how far back we should jump
$seek = min(ftell($f), $buffer);
// Do the jump (backwards, relative to where we are)
fseek($f, -$seek, SEEK_CUR);
// Read a chunk and prepend it to our output
$chunk = fread($f, $seek);
if ($chunk === false) {
throw new \RuntimeException('Cannot read file');
}
$output = $chunk . $output;
// Jump back to where we started reading
fseek($f, -mb_strlen($chunk, '8bit'), SEEK_CUR);
// Decrease our line counter
$lines -= substr_count($chunk, "\n");
}
// While we have too many lines
// (Because of buffer size we might have read too many)
while ($lines++ < 0) {
// Find first newline and remove all text before that
$output = substr($output, strpos($output, "\n") + 1);
}
// Close file and return
fclose($f);
return trim($output);
}
/**
* Helper class to get level color
*
* @param string $level
* @return string
*/
public static function levelColor($level)
{
$colors = [
'DEBUG' => 'green',
'INFO' => 'cyan',
'NOTICE' => 'yellow',
'WARNING' => 'yellow',
'ERROR' => 'red',
'CRITICAL' => 'red',
'ALERT' => 'red',
'EMERGENCY' => 'magenta'
];
return $colors[$level] ?? 'white';
}
/**
* Parse a monolog row into array bits
*
* @param string $line
* @return array
*/
public function parse($line)
{
if (!is_string($line) || $line === '') {
return [];
}
preg_match($this->pattern, $line, $data);
if (!isset($data['date'])) {
return [];
}
preg_match('/(.*)- Trace:(.*)/', $data['message'], $matches);
if (is_array($matches) && isset($matches[1])) {
$data['message'] = trim($matches[1]);
$data['trace'] = trim($matches[2]);
}
try {
$date = new DateTime($data['date']);
} catch (DateMalformedStringException $e) {
$date = null;
}
return [
'date' => $date,
'logger' => $data['logger'],
'level' => $data['level'],
'message' => $data['message'],
'trace' => isset($data['trace']) ? self::parseTrace($data['trace']) : null,
'context' => json_decode($data['context'], true),
'extra' => json_decode($data['extra'], true)
];
}
/**
* Parse text of trace into an array of lines
*
* @param string $trace
* @param int $rows
* @return array
*/
public static function parseTrace($trace, $rows = 10)
{
$lines = array_filter(preg_split('/#\d*/m', $trace));
return array_slice($lines, 0, $rows);
}
}