1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace Cion\LaravelLogReader\Reader; |
5
|
|
|
|
6
|
|
|
use Carbon\Carbon; |
7
|
|
|
|
8
|
|
|
class LogSectionReader |
9
|
|
|
{ |
10
|
|
|
protected $lineHelper; |
11
|
|
|
|
12
|
|
|
protected $types = [ |
13
|
|
|
'DEBUG', |
14
|
|
|
'EMERGENCY', |
15
|
|
|
'ALERT', |
16
|
|
|
'CRITICAL', |
17
|
|
|
'ERROR', |
18
|
|
|
'WARNING', |
19
|
|
|
'NOTICE', |
20
|
|
|
'INFO', |
21
|
|
|
]; |
22
|
|
|
|
23
|
|
|
public $type; |
24
|
|
|
|
25
|
|
|
public $date; |
26
|
|
|
|
27
|
|
|
public $message; |
28
|
|
|
|
29
|
|
|
public $extra; |
30
|
|
|
|
31
|
|
|
public function __construct(array $section) |
32
|
|
|
{ |
33
|
|
|
$this->lineHelper = new LineHelper($section['line']); |
34
|
|
|
|
35
|
|
|
$this->extra = collect($section['extra']); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function read() : LogSectionReader |
39
|
|
|
{ |
40
|
|
|
$this->retrieveSectionInformations(); |
41
|
|
|
|
42
|
|
|
return $this; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function handleType() : void |
46
|
|
|
{ |
47
|
|
|
if (request()->filled('logreader_type') && request('logreader_type') !== 'all') { |
48
|
|
|
$this->types = [strtoupper(request()->logreader_type)]; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
foreach ($this->types as $type) { |
52
|
|
|
if ($this->lineHelper->hasType($type)) { |
53
|
|
|
$this->type = $type; |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function handleDate() : void |
59
|
|
|
{ |
60
|
|
|
if (strtotime($date = $this->lineHelper->getDate())) { |
61
|
|
|
$this->date = Carbon::parse($date); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function handleMessage() : void |
66
|
|
|
{ |
67
|
|
|
$this->message = $this->lineHelper->getLogMessageOf($this->type); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function retrieveSectionInformations() : void |
71
|
|
|
{ |
72
|
|
|
$this->handleDate(); |
73
|
|
|
|
74
|
|
|
$this->handleType(); |
75
|
|
|
|
76
|
|
|
$this->handleMessage(); |
77
|
|
|
|
78
|
|
|
$this->handleExtra(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function handleExtra() : void |
82
|
|
|
{ |
83
|
|
|
// Remove Useless Informations |
84
|
|
|
$this->extra->shift(); |
85
|
|
|
$this->extra->pop(); |
86
|
|
|
$this->extra->pop(); |
87
|
|
|
|
88
|
|
|
$this->extra = $this->extra->map(function ($extra) { |
89
|
|
|
return explode(': ', substr($extra, 3)); |
90
|
|
|
}); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function toArray() : array |
94
|
|
|
{ |
95
|
|
|
if (! $this->date || ! $this->type || ! $this->message || ! $this->extra) { |
96
|
|
|
return []; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return [ |
100
|
|
|
'date' => $this->date, |
101
|
|
|
'type' => $this->type, |
102
|
|
|
'message' => $this->message, |
103
|
|
|
'extra' => $this->extra->toArray() |
104
|
|
|
]; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|