|
1
|
|
|
<?php namespace Arcanesoft\Foundation\Http\Controllers\Admin\System; |
|
2
|
|
|
|
|
3
|
|
|
use Arcanedev\LaravelApiHelper\Traits\JsonResponses; |
|
4
|
|
|
use Arcanedev\LogViewer\Contracts\LogViewer as LogViewerContract; |
|
5
|
|
|
use Arcanedev\LogViewer\Entities\Log; |
|
6
|
|
|
use Arcanedev\LogViewer\Entities\LogEntry; |
|
7
|
|
|
use Arcanesoft\Foundation\Policies\LogViewerPolicy; |
|
8
|
|
|
use Illuminate\Http\Request; |
|
9
|
|
|
use Illuminate\Pagination\LengthAwarePaginator; |
|
10
|
|
|
use Illuminate\Support\Arr; |
|
11
|
|
|
use Illuminate\Support\Str; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class LogViewerController |
|
15
|
|
|
* |
|
16
|
|
|
* @package Arcanesoft\Foundation\Http\Controllers\Admin\System |
|
17
|
|
|
* @author ARCANEDEV <[email protected]> |
|
18
|
|
|
*/ |
|
19
|
|
|
class LogViewerController extends Controller |
|
20
|
|
|
{ |
|
21
|
|
|
/* ----------------------------------------------------------------- |
|
22
|
|
|
| Trait |
|
23
|
|
|
| ----------------------------------------------------------------- |
|
24
|
|
|
*/ |
|
25
|
|
|
|
|
26
|
|
|
use JsonResponses; |
|
27
|
|
|
|
|
28
|
|
|
/* ----------------------------------------------------------------- |
|
29
|
|
|
| Properties |
|
30
|
|
|
| ----------------------------------------------------------------- |
|
31
|
|
|
*/ |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* The LogViewer instance. |
|
35
|
|
|
* |
|
36
|
|
|
* @var \Arcanedev\LogViewer\Contracts\LogViewer |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $logViewer; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Logs per page. |
|
42
|
|
|
* |
|
43
|
|
|
* @var int |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $perPage = 30; |
|
46
|
|
|
|
|
47
|
|
|
/* ----------------------------------------------------------------- |
|
48
|
|
|
| Constructor |
|
49
|
|
|
| ----------------------------------------------------------------- |
|
50
|
|
|
*/ |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* LogViewerController constructor. |
|
54
|
|
|
* |
|
55
|
|
|
* @param \Arcanedev\LogViewer\Contracts\LogViewer $logViewer |
|
56
|
|
|
*/ |
|
57
|
|
|
public function __construct(LogViewerContract $logViewer) |
|
58
|
|
|
{ |
|
59
|
|
|
parent::__construct(); |
|
60
|
|
|
|
|
61
|
|
|
$this->logViewer = $logViewer; |
|
62
|
|
|
$this->perPage = config('arcanesoft.foundation.log-viewer.per-page', $this->perPage); |
|
63
|
|
|
$this->setCurrentPage('foundation-system-logviewer'); |
|
64
|
|
|
$this->addBreadcrumbRoute('LogViewer', 'admin::foundation.system.log-viewer.index'); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
68
|
|
|
| Main Functions |
|
69
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
70
|
|
|
*/ |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Show the LogViewer Dashboard. |
|
74
|
|
|
* |
|
75
|
|
|
* @return \Illuminate\View\View |
|
76
|
|
|
*/ |
|
77
|
|
|
public function index() |
|
78
|
|
|
{ |
|
79
|
|
|
$this->authorize(LogViewerPolicy::PERMISSION_DASHBOARD); |
|
|
|
|
|
|
80
|
|
|
|
|
81
|
|
|
$stats = $this->logViewer->statsTable(); |
|
82
|
|
|
$percents = $this->calcPercentages($stats->footer(), $stats->header()); |
|
83
|
|
|
|
|
84
|
|
|
$this->setTitle('LogViewer Dashboard'); |
|
85
|
|
|
$this->addBreadcrumb('Dashboard'); |
|
86
|
|
|
|
|
87
|
|
|
return $this->view('admin.system.log-viewer.dashboard', compact('percents')); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* List all logs. |
|
92
|
|
|
* |
|
93
|
|
|
* @param \Illuminate\Http\Request $request |
|
94
|
|
|
* |
|
95
|
|
|
* @return \Illuminate\View\View |
|
96
|
|
|
*/ |
|
97
|
|
|
public function listLogs(Request $request) |
|
98
|
|
|
{ |
|
99
|
|
|
$this->authorize(LogViewerPolicy::PERMISSION_LIST); |
|
|
|
|
|
|
100
|
|
|
|
|
101
|
|
|
$stats = $this->logViewer->statsTable(); |
|
102
|
|
|
$headers = $stats->header(); |
|
103
|
|
|
// $footer = $stats->footer(); |
|
104
|
|
|
|
|
105
|
|
|
$page = $request->get('page', 1); |
|
106
|
|
|
$offset = ($page * $this->perPage) - $this->perPage; |
|
107
|
|
|
$rows = new LengthAwarePaginator( |
|
108
|
|
|
array_slice($stats->rows(), $offset, $this->perPage, true), |
|
109
|
|
|
count($stats->rows()), |
|
110
|
|
|
$this->perPage, |
|
111
|
|
|
$page |
|
112
|
|
|
); |
|
113
|
|
|
$rows->setPath($request->url()); |
|
114
|
|
|
|
|
115
|
|
|
$this->setTitle($title = trans('foundation::log-viewer.titles.logs-list')); |
|
116
|
|
|
$this->addBreadcrumb($title); |
|
117
|
|
|
|
|
118
|
|
|
return $this->view('admin.system.log-viewer.list', compact('headers', 'rows', 'footer')); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Show the log entries by date. |
|
123
|
|
|
* |
|
124
|
|
|
* @param \Arcanedev\LogViewer\Entities\Log $log |
|
125
|
|
|
* |
|
126
|
|
|
* @return \Illuminate\View\View |
|
127
|
|
|
*/ |
|
128
|
|
|
public function show(Log $log) |
|
129
|
|
|
{ |
|
130
|
|
|
$this->authorize(LogViewerPolicy::PERMISSION_SHOW); |
|
|
|
|
|
|
131
|
|
|
|
|
132
|
|
|
$levels = $this->logViewer->levelsNames(); |
|
133
|
|
|
$entries = $log->entries($level = 'all')->paginate($this->perPage); |
|
134
|
|
|
|
|
135
|
|
|
$this->addBreadcrumbRoute(trans('foundation::log-viewer.titles.logs-list'), 'admin::foundation.system.log-viewer.logs.list'); |
|
136
|
|
|
$this->setTitle($title = "Log : {$log->date}"); |
|
137
|
|
|
$this->addBreadcrumb($title); |
|
138
|
|
|
|
|
139
|
|
|
return $this->view('admin.system.log-viewer.show', compact('log', 'levels', 'level', 'search', 'entries')); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Filter the log entries by date and level. |
|
144
|
|
|
* |
|
145
|
|
|
* @param \Arcanedev\LogViewer\Entities\Log $log |
|
146
|
|
|
* @param string $level |
|
147
|
|
|
* |
|
148
|
|
|
* @return \Illuminate\View\View|\Illuminate\Http\RedirectResponse |
|
149
|
|
|
*/ |
|
150
|
|
|
public function showByLevel(Log $log, $level) |
|
151
|
|
|
{ |
|
152
|
|
|
$this->authorize(LogViewerPolicy::PERMISSION_SHOW); |
|
|
|
|
|
|
153
|
|
|
|
|
154
|
|
|
if ($level == 'all') |
|
155
|
|
|
return redirect()->route('admin::foundation.system.log-viewer.logs.show', [$log->date]); |
|
156
|
|
|
|
|
157
|
|
|
$levels = $this->logViewer->levelsNames(); |
|
158
|
|
|
$entries = $this->logViewer->entries($log->date, $level)->paginate($this->perPage); |
|
159
|
|
|
|
|
160
|
|
|
$this->addBreadcrumbRoute(trans('foundation::log-viewer.titles.logs-list'), 'admin::foundation.system.log-viewer.logs.list'); |
|
161
|
|
|
|
|
162
|
|
|
$this->setTitle($log->date.' | '.ucfirst($level)); |
|
163
|
|
|
$this->addBreadcrumbRoute($log->date, 'admin::foundation.system.log-viewer.logs.show', [$log->date]); |
|
164
|
|
|
$this->addBreadcrumb(ucfirst($level)); |
|
165
|
|
|
|
|
166
|
|
|
return $this->view('admin.system.log-viewer.show', compact('log', 'levels', 'entries', 'level')); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Show the log with the search query. |
|
171
|
|
|
* |
|
172
|
|
|
* @param \Arcanedev\LogViewer\Entities\Log $log |
|
173
|
|
|
* @param string $level |
|
174
|
|
|
* @param \Illuminate\Http\Request $request |
|
175
|
|
|
* |
|
176
|
|
|
* @return \Illuminate\View\View |
|
177
|
|
|
*/ |
|
178
|
|
|
public function search(Log $log, $level = 'all', Request $request) |
|
179
|
|
|
{ |
|
180
|
|
|
if (is_null($query = $request->get('query'))) |
|
181
|
|
|
return redirect()->route('admin::foundation.system.log-viewer.logs.show', [$log->date]); |
|
|
|
|
|
|
182
|
|
|
|
|
183
|
|
|
$levels = $this->logViewer->levelsNames(); |
|
184
|
|
|
$entries = $log->entries($level)->filter(function (LogEntry $value) use ($query) { |
|
185
|
|
|
return Str::contains($value->header, $query); |
|
186
|
|
|
})->paginate($this->perPage); |
|
187
|
|
|
|
|
188
|
|
|
return $this->view('admin.system.log-viewer.show', compact('log', 'levels', 'level', 'query', 'entries')); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* Download the log. |
|
193
|
|
|
* |
|
194
|
|
|
* @param \Arcanedev\LogViewer\Entities\Log $log |
|
195
|
|
|
* |
|
196
|
|
|
* @return \Symfony\Component\HttpFoundation\BinaryFileResponse |
|
197
|
|
|
*/ |
|
198
|
|
|
public function download(Log $log) |
|
199
|
|
|
{ |
|
200
|
|
|
$this->authorize(LogViewerPolicy::PERMISSION_DOWNLOAD); |
|
|
|
|
|
|
201
|
|
|
|
|
202
|
|
|
return $this->logViewer->download($log->date); |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* Delete a log. |
|
207
|
|
|
* |
|
208
|
|
|
* @param \Arcanedev\LogViewer\Entities\Log $log |
|
209
|
|
|
* |
|
210
|
|
|
* @return \Illuminate\Http\JsonResponse |
|
211
|
|
|
*/ |
|
212
|
|
|
public function delete(Log $log) |
|
213
|
|
|
{ |
|
214
|
|
|
$this->authorize(LogViewerPolicy::PERMISSION_DELETE); |
|
|
|
|
|
|
215
|
|
|
|
|
216
|
|
|
$date = $log->date; |
|
217
|
|
|
|
|
218
|
|
|
if ($this->logViewer->delete($date)) { |
|
219
|
|
|
$this->notifySuccess( |
|
220
|
|
|
$message = trans('foundation::log-viewer.messages.deleted.message', compact('date')), |
|
221
|
|
|
trans('foundation::log-viewer.messages.deleted.title') |
|
222
|
|
|
); |
|
223
|
|
|
|
|
224
|
|
|
return $this->jsonResponseSuccess(compact('message')); |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
return $this->jsonResponseError([ |
|
228
|
|
|
'message' => "An error occurred while deleting the log [$date]" |
|
229
|
|
|
]); |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
/* ----------------------------------------------------------------- |
|
233
|
|
|
| Other Methods |
|
234
|
|
|
| ----------------------------------------------------------------- |
|
235
|
|
|
*/ |
|
236
|
|
|
|
|
237
|
|
|
/** |
|
238
|
|
|
* Calculate the percentage. |
|
239
|
|
|
* |
|
240
|
|
|
* @param array $total |
|
241
|
|
|
* @param array $names |
|
242
|
|
|
* |
|
243
|
|
|
* @return array |
|
244
|
|
|
*/ |
|
245
|
|
|
private function calcPercentages(array $total, array $names) |
|
246
|
|
|
{ |
|
247
|
|
|
$percents = []; |
|
248
|
|
|
$all = Arr::get($total, 'all'); |
|
249
|
|
|
|
|
250
|
|
|
foreach ($total as $level => $count) { |
|
251
|
|
|
$percents[$level] = [ |
|
252
|
|
|
'name' => $names[$level], |
|
253
|
|
|
'count' => $count, |
|
254
|
|
|
'percent' => round(($count / $all) * 100, 2), |
|
255
|
|
|
]; |
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
|
|
return $percents; |
|
259
|
|
|
} |
|
260
|
|
|
} |
|
261
|
|
|
|
If you implement
__calland you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__callis implemented by a parent class and only the child class knows which methods exist: