1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MoriTiza\LaraLog\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use App\Http\Controllers\Controller; |
6
|
|
|
use Illuminate\Support\Facades\Log; |
7
|
|
|
use Illuminate\Http\Request; |
8
|
|
|
use Illuminate\Pagination\LengthAwarePaginator; |
9
|
|
|
|
10
|
|
|
class LaraLogController extends Controller |
11
|
|
|
{ |
12
|
|
|
private $singleLogs = array(); |
13
|
|
|
private $dailyLogs = array(); |
14
|
|
|
private $dailyLogsFiles = array(); |
15
|
|
|
|
16
|
|
|
public function __construct() |
17
|
|
|
{ |
18
|
|
|
$this->middleware('auth'); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function index(Request $request) |
22
|
|
|
{ |
23
|
|
|
$this->singleLogsHandler(); |
24
|
|
|
$this->dailyLogsHandler(); |
25
|
|
|
|
26
|
|
|
$singleLogs = array_reverse($this->singleLogs); |
27
|
|
|
$dailyLogs = array_reverse($this->dailyLogs); |
28
|
|
|
|
29
|
|
|
$logQuery = $request->input('log'); |
30
|
|
|
|
31
|
|
|
if (isset($logQuery) && (! is_null($logQuery)) && array_key_exists($logQuery, $this->dailyLogs)) { |
32
|
|
|
$currentLog = $logQuery; |
33
|
|
|
} elseif (file_exists(storage_path('logs/laravel.log'))) { // me |
34
|
|
|
$currentLog = 'laravel'; |
35
|
|
|
} elseif (count($dailyLogs) > 0) { // me |
36
|
|
|
$currentLog = array_key_first($dailyLogs); //me |
37
|
|
|
} else { |
38
|
|
|
$currentLog = null; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
if ($currentLog === 'laravel') { |
42
|
|
|
$currentPage = LengthAwarePaginator::resolveCurrentPage(); |
43
|
|
|
$logsCollection = collect($singleLogs); |
44
|
|
|
$perPage = 5; |
45
|
|
|
$currentPageItems = $logsCollection->slice(($currentPage * $perPage) - $perPage, $perPage)->all(); |
46
|
|
|
$paginatedSingleLogs = new LengthAwarePaginator($currentPageItems , count($logsCollection), $perPage); |
47
|
|
|
$paginatedSingleLogs->setPath($request->url()); |
48
|
|
|
} elseif (isset($logQuery) && (! is_null($logQuery)) && array_key_exists($logQuery, $this->dailyLogs)) { |
49
|
|
|
$currentPage = LengthAwarePaginator::resolveCurrentPage(); |
50
|
|
|
$logsCollection = collect(array_reverse($dailyLogs[$logQuery])); |
51
|
|
|
$perPage = 5; |
52
|
|
|
$currentPageItems = $logsCollection->slice(($currentPage * $perPage) - $perPage, $perPage)->all(); |
53
|
|
|
$paginatedDailyLogs = new LengthAwarePaginator($currentPageItems , count($logsCollection), $perPage); |
54
|
|
|
$paginatedDailyLogs->setPath('logs?log=' . $currentLog); |
55
|
|
|
} elseif (count($dailyLogs) > 0) { |
56
|
|
|
$currentPage = LengthAwarePaginator::resolveCurrentPage(); |
57
|
|
|
$logsCollection = collect(array_reverse($dailyLogs[$currentLog])); |
58
|
|
|
$perPage = 5; |
59
|
|
|
$currentPageItems = $logsCollection->slice(($currentPage * $perPage) - $perPage, $perPage)->all(); |
60
|
|
|
$paginatedDailyLogs = new LengthAwarePaginator($currentPageItems , count($logsCollection), $perPage); |
61
|
|
|
$paginatedDailyLogs->setPath('logs?log=' . $currentLog); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if (isset($paginatedDailyLogs)) { |
65
|
|
|
return view('laralog::logs', compact('singleLogs', 'dailyLogs', 'currentLog', 'paginatedDailyLogs')); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return view('laralog::logs', compact('singleLogs', 'dailyLogs', 'currentLog', 'paginatedSingleLogs')); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
private function singleLogsHandler() |
72
|
|
|
{ |
73
|
|
|
if (file_exists(storage_path('logs/laravel.log'))) { |
|
|
|
|
74
|
|
|
$contents = file(storage_path('logs/laravel.log')); |
75
|
|
|
|
76
|
|
|
foreach ($contents as $key => $value) { |
77
|
|
|
$this->singleLogs[] = trim(preg_replace('/\s\s+/', ' ', $value)); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$this->deleteAdditionalSingleLogs(); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
private function dailyLogsHandler() |
85
|
|
|
{ |
86
|
|
|
$files = scandir(storage_path('logs')); |
87
|
|
|
|
88
|
|
|
foreach ($files as $key => $value) { |
89
|
|
|
if (preg_match("/^laravel-20(1[9]|2[0-9])-(0[1-9]|1[0,1,2])-(0[1-9]|1[0-9]|2[0-9]|3[0,1])['.']log$/", $value)) { |
90
|
|
|
$this->dailyLogsFiles[] = $value; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
if (count($this->dailyLogsFiles) > 0) { |
95
|
|
|
foreach ($this->dailyLogsFiles as $key => $value) { |
96
|
|
|
|
97
|
|
|
$dailyLogsKey = substr($value, 8, 10); |
98
|
|
|
$this->dailyLogs[$dailyLogsKey] = array(); |
99
|
|
|
|
100
|
|
|
$contents = file(storage_path('logs/' . $value)); |
101
|
|
|
|
102
|
|
|
foreach ($contents as $contentsKey => $contentsValue) { |
103
|
|
|
$this->dailyLogs[$dailyLogsKey][] = trim(preg_replace('/\s\s+/', ' ', $contentsValue)); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$this->dailyLogs[$dailyLogsKey] = $this->deleteAdditionalDailyLogs($this->dailyLogs[$dailyLogsKey]); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
|
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
private function deleteAdditionalSingleLogs() |
114
|
|
|
{ |
115
|
|
|
foreach ($this->singleLogs as $key => $value) { |
116
|
|
|
if ($value === trim('[stacktrace]') || $value === trim('"}') || trim($value) == null || preg_match('/^#[0-9]+[\' \']/', $value) || preg_match('/^\[previous exception\] \[object\]/', $value)) { |
117
|
|
|
unset($this->singleLogs[$key]); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
private function deleteAdditionalDailyLogs($dailyLogs) |
123
|
|
|
{ |
124
|
|
|
foreach ($dailyLogs as $key => $value) { |
125
|
|
|
if ($value === trim('[stacktrace]') || $value === trim('"}') || trim($value) == null || preg_match('/^#[0-9]+[\' \']/', $value) || preg_match('/^\[previous exception\] \[object\]/', $value)) { |
126
|
|
|
unset($dailyLogs[$key]); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return $dailyLogs; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|