|
<?php |
|
|
|
namespace App\Listeners; |
|
|
|
use Illuminate\Events\Dispatcher; |
|
use Illuminate\Foundation\Http\Events\RequestHandled; |
|
use Illuminate\Console\Events\CommandFinished; |
|
use Illuminate\Http\Client\Events\ResponseReceived; |
|
use Illuminate\Support\Facades\Log; |
|
|
|
class ActivityLogEventSubscriber |
|
{ |
|
public function handleHttpRequest(RequestHandled $event): void |
|
{ |
|
// Log activity for HTTP requests |
|
$this->logActivity('RequestHandled', [ |
|
'request' => $event->request->toArray(), |
|
'response' => $event->response->toArray(), |
|
]); |
|
} |
|
|
|
public function handleConsoleCommand(CommandFinished $event): void |
|
{ |
|
// Log activity for console commands |
|
$this->logActivity('CommandFinished', [ |
|
'command' => $event->command, |
|
'exitCode' => $event->exitCode, |
|
]); |
|
} |
|
|
|
public function handleHttpClientResponse(ResponseReceived $event): void |
|
{ |
|
// Log activity for HTTP client responses |
|
$this->logActivity('ResponseReceived', [ |
|
'request' => $event->request->toArray(), |
|
'response' => $event->response->toArray(), |
|
]); |
|
} |
|
|
|
private function logActivity(string $eventType, array $eventData): void |
|
{ |
|
// You can customize the log data based on their specific requirements |
|
// For example, You may want to include additional context, user information, or any other relevant details. |
|
Log::info("Activity Log: $eventType - " . json_encode($eventData)); |
|
} |
|
|
|
public function subscribe(Dispatcher $events): array |
|
{ |
|
return [ |
|
RequestHandled::class => 'handleHttpRequest', |
|
CommandFinished::class => 'handleConsoleCommand', |
|
ResponseReceived::class => 'handleHttpClientResponse', |
|
]; |
|
} |
|
} |
check some examples by @aniket-magadum
RequestHandled: https://gist.github.com/aniket-magadum/947cd07329dfb618a9c04dfdd425dfc8
CommandFinished: https://gist.github.com/aniket-magadum/9e56e4d888587c4632bc9bfc712586c1
ResponseReceived: https://gist.github.com/aniket-magadum/23d91888bd4d1071280d11562d3884d7