logKey = [ // 'log_key' => (int) $controller->keyLogs(), // 'caller' => 'CLI' // ]; // $request = new Request([ // 'param1' => 'value1', // 'param2' => 'value2', // ]); // $result = $controller->extendRepair($request); $echokey = Carbon::now()->format('YmdHisv') . str_replace('-', '', Str::uuid()->toString()); echo Carbon::now()->format('Y-m-d H:i:s') . ' ::key' . $echokey . ' => ' . ('=======================start') . PHP_EOL; //get data system error $system_errors = DB::table('system_errors') ->orderBy('id', 'asc') ->limit(20) ->get(); //get data logs yg levelnya ERROR $logs = DB::table('system_logs') ->where('send_mail', 0) ->whereRaw("TRIM(level) = '500'") ->orderBy('id', 'asc') ->limit(20) ->get(); if ($system_errors->isEmpty() and $logs->isEmpty()) { echo Carbon::now()->format('Y-m-d H:i:s') . ' ::key' . $echokey . ' => ' . ('Tidak ada log yang perlu dikirim') . PHP_EOL; return; } // Simpan daftar ID yang diproses $system_error_ids = $system_errors->pluck('id')->toArray(); $log_ids = $logs->pluck('id')->toArray(); // Buat isi file txt $content = ""; foreach ($system_errors as $log) { $content .= "SOURCE : table system_errors\n"; $content .= "ID: {$log->id}\n"; $content .= "Type: {$log->type}\n"; $content .= "Created At: {$log->created_at}\n"; $content .= "Message: {$log->message}\n"; $content .= "File: {$log->file}\n"; $content .= "Line: {$log->line}\n"; $content .= "Trace: {$log->trace}\n"; $content .= str_repeat("=", 50) . "\n\n"; } foreach ($logs as $log) { $content .= "SOURCE : table system_logs\n"; $content .= "ID: {$log->id}\n"; $content .= "Type: {$log->name_log}\n"; $content .= "Created At: {$log->datetime}\n"; $content .= "Message: {$log->message}\n"; $content .= "File: {$log->class}\n"; $content .= "Line: {unknown}\n"; $content .= "Trace: {$log->trace}\n"; $content .= str_repeat("=", 50) . "\n\n"; } // Simpan ke storage $fileName = 'system_errors_' . now()->format('Ymd_His') . '.txt'; Storage::disk('local')->put($fileName, $content); $filePath = storage_path('app/' . $fileName); try { // Kirim email // Ambil value dari parameter_globals $emailsString = \DB::table('parameter_globals') ->where('key_name', 'ERROR_INFO_EMAILS') ->value('value'); // langsung ambil kolom value if ($emailsString) { // Pisahkan dengan koma & trim spasi $emails = array_map('trim', explode(',', $emailsString)); Mail::raw( "Halo Tim,\n\n" . "Telah terjadi error pada sistem. Detail error terlampir pada file .txt di email ini.\n\n" . "Mohon segera ditindaklanjuti.\n\n" . "Salam,\n" . "Laravel Error Reporter", function ($message) use ($filePath, $emails) { $message->to($emails) ->subject('Error Logs - ' . now()->format('Y-m-d H:i')) ->attach($filePath); } ); // Hapus data yang sudah diproses DB::table('system_errors') ->whereIn('id', $system_error_ids) ->delete(); // Update data yang sudah diproses DB::table('system_logs') ->whereIn('id', $log_ids) ->update(['send_mail' => 1]); echo Carbon::now()->format('Y-m-d H:i:s') . ' ::key' . $echokey . ' => ' . ('Email berhasil dikirim.') . PHP_EOL; } else { echo Carbon::now()->format('Y-m-d H:i:s') . ' ::key' . $echokey . ' => ' . ('Email penerima notifikasi tidak ditemukan di parameter_globals') . PHP_EOL; } } catch (\Exception $e) { echo Carbon::now()->format('Y-m-d H:i:s') . ' ::key' . $echokey . ' => ' . ('Gagal mengirim email: ' . $e->getMessage()) . PHP_EOL; echo Carbon::now()->format('Y-m-d H:i:s') . ' ::key' . $echokey . ' => ' . ('Gagal mengirim email: ' . $e->getTraceAsString()) . PHP_EOL; } echo Carbon::now()->format('Y-m-d H:i:s') . ' ::key' . $echokey . ' => ' . ('=======================end') . PHP_EOL; } }