feat(logs): tambahkan SystemLogsController untuk mengelola log sistem
- Menambahkan kelas SystemLogsController untuk menampilkan dan mengelola log sistem. - Mengimplementasikan metode index untuk menampilkan tampilan log. - Menambahkan metode datatable untuk mengelola data log dengan fitur pencarian dan paginasi. - Menggunakan LogReader untuk mengambil dan memfilter data log.
This commit is contained in:
79
app/Http/Controllers/SystemLogsController.php
Normal file
79
app/Http/Controllers/SystemLogsController.php
Normal file
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Logs\Http\Controllers;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
use Jackiedo\LogReader\Exceptions\UnableToRetrieveLogFilesException;
|
||||
use Jackiedo\LogReader\LogReader;
|
||||
|
||||
class SystemLogsController extends Controller
|
||||
{
|
||||
protected $reader;
|
||||
|
||||
public function __construct(LogReader $reader)
|
||||
{
|
||||
$this->reader = $reader;
|
||||
}
|
||||
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
return view('logs::system');
|
||||
}
|
||||
|
||||
public function datatable(Request $request){
|
||||
$data = collect();
|
||||
$this->reader->setLogPath(storage_path('logs'));
|
||||
try {
|
||||
$data = $this->reader->get()->merge($data);
|
||||
} catch (UnableToRetrieveLogFilesException $exception) {
|
||||
echo $exception->getMessage();
|
||||
exit;
|
||||
}
|
||||
|
||||
$data = $data->map(function ($a) {
|
||||
return (collect($a))->only(['id', 'date', 'environment', 'level', 'file_path', 'context']);
|
||||
});
|
||||
|
||||
|
||||
// Get pagination parameters from request
|
||||
$perPage = (int) $request->input('size', 10);
|
||||
$currentPage = (int) $request->input('page', 1);
|
||||
$search = $request->input('search', '');
|
||||
|
||||
// Apply search if provided
|
||||
if (!empty($search)) {
|
||||
$data = $data->filter(function ($item) use ($search) {
|
||||
// Search in relevant fields
|
||||
return stripos($item['level'], $search) !== false ||
|
||||
stripos($item['environment'], $search) !== false ||
|
||||
stripos($item['id'], $search) !== false ||
|
||||
stripos($item['context'], $search) !== false;
|
||||
});
|
||||
}
|
||||
|
||||
// Get total count before pagination
|
||||
$totalRecords = $data->count();
|
||||
$filteredRecords = $totalRecords; // Same as total if no filtering applied
|
||||
|
||||
// Apply pagination
|
||||
$offset = ($currentPage - 1) * $perPage;
|
||||
$data = $data->slice($offset, $perPage)->values();
|
||||
|
||||
// Calculate page count
|
||||
$pageCount = ceil($filteredRecords / $perPage);
|
||||
|
||||
return response()->json([
|
||||
'draw' => (int) $request->input('draw', 1),
|
||||
'recordsTotal' => $totalRecords,
|
||||
'recordsFiltered' => $filteredRecords,
|
||||
'pageCount' => $pageCount,
|
||||
'page' => $currentPage,
|
||||
'totalCount' => $filteredRecords,
|
||||
'data' => $data,
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user