1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Cion\LaravelLogReader\Reader; |
4
|
|
|
|
5
|
|
|
use Cion\LaravelLogReader\Reader\Exception\FolderNotFoundException; |
6
|
|
|
use Illuminate\Filesystem\Filesystem; |
7
|
|
|
|
8
|
|
|
class LogReader |
9
|
|
|
{ |
10
|
|
|
protected $filesystem; |
11
|
|
|
|
12
|
|
|
protected $files = []; |
13
|
|
|
|
14
|
|
|
protected $time; |
15
|
|
|
|
16
|
|
|
public $loggers = []; |
17
|
|
|
|
18
|
|
|
public function __construct() |
19
|
|
|
{ |
20
|
|
|
$this->filesystem = new Filesystem; |
21
|
|
|
|
22
|
|
|
$this->time = now(); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function read() |
26
|
|
|
{ |
27
|
|
|
$this->getFiles()->each(function ($file) { |
28
|
|
|
$this->getFileLines($file)->each(function ($line) { |
29
|
|
|
if ($lineHandler = (new LineReader($line))->handle()) { |
30
|
|
|
$this->loggers[] = $lineHandler->getArray(); |
31
|
|
|
} |
32
|
|
|
}); |
33
|
|
|
}); |
34
|
|
|
|
35
|
|
|
return $this; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function toArray() |
39
|
|
|
{ |
40
|
|
|
return $this->loggers; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function toDatabase() |
44
|
|
|
{ |
45
|
|
|
// specify table or model |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function get() |
49
|
|
|
{ |
50
|
|
|
// TODO:Add Expection if this path doesn't exists |
51
|
|
|
if (! $this->filesystem->exists(config('logreader.path'))) { |
52
|
|
|
throw new FolderNotFoundException(); |
53
|
|
|
// throw new Exception("File Does Not Exists"); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return $this->getDiretoryFiles()->getSubDirectoriesFiles()->getFiles(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function getFiles() |
60
|
|
|
{ |
61
|
|
|
return collect($this->files)->filter(function ($file) { |
62
|
|
|
if (! is_null($this->getTime())) { |
63
|
|
|
// Get only the files that has the current time |
64
|
|
|
return $file->getFilename() === "laravel-{$this->getTime()}.log"; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return true; |
68
|
|
|
}); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function getSubDirectoriesFiles() |
72
|
|
|
{ |
73
|
|
|
collect($this->filesystem->directories(config('logreader.path')))->map(function ($directory) { |
74
|
|
|
collect($this->filesystem->files($directory))->filter(function ($file) { |
75
|
|
|
$this->files[] = $file; |
76
|
|
|
}); |
77
|
|
|
}); |
78
|
|
|
|
79
|
|
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function getDiretoryFiles() |
83
|
|
|
{ |
84
|
|
|
collect($this->filesystem->files(config('logreader.path')))->filter(function ($file) { |
85
|
|
|
$this->files[] = $file; |
86
|
|
|
}); |
87
|
|
|
|
88
|
|
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function getFileLines($file) |
92
|
|
|
{ |
93
|
|
|
return collect(explode("\n", $this->filesystem->get($file->getPathname()))); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function all() |
97
|
|
|
{ |
98
|
|
|
$this->time = null; |
99
|
|
|
|
100
|
|
|
return $this; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function yesterday() |
104
|
|
|
{ |
105
|
|
|
$this->time = now()->yesterday(); |
106
|
|
|
|
107
|
|
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
protected function getTime() |
111
|
|
|
{ |
112
|
|
|
if (is_null($this->time)) { |
113
|
|
|
return null; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return $this->time->format('Y-m-d'); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|