Skip to content

Instantly share code, notes, and snippets.

@tprinty
Last active July 14, 2024 04:32
Show Gist options
  • Save tprinty/ba5e67b084ba2189a2dab4ae19be89dc to your computer and use it in GitHub Desktop.
Save tprinty/ba5e67b084ba2189a2dab4ae19be89dc to your computer and use it in GitHub Desktop.
Add Logging to Lavavel Authentication
Here is a quick way to add authentication logging to Laravel.
1. Modify app/Providers/EventServiceProvider.php and add lines 16 through 32 of the example file in this GIST.
2. Create a new file app/Listeners/LogActivity.php and copy the contents of the file below into that file.
3. Enjoy logging.
<?php
namespace App\Providers;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Event;
class EventServiceProvider extends ServiceProvider
{
/**
* The event listener mappings for the application.
*
* @var array
*/
protected $listen = [
\Illuminate\Auth\Events\Login::class => [
\App\Listeners\LogActivity::class.'@login',
],
\Illuminate\Auth\Events\Logout::class => [
\App\Listeners\LogActivity::class.'@logout',
],
\Illuminate\Auth\Events\Registered::class => [
\Illuminate\Auth\Listeners\SendEmailVerificationNotification::class,
\App\Listeners\LogActivity::class.'@registered',
],
\Illuminate\Auth\Events\Failed::class => [
\App\Listeners\LogActivity::class.'@failed',
],
\Illuminate\Auth\Events\PasswordReset::class => [
\App\Listeners\LogActivity::class.'@passwordReset',
]
];
/**
* Register any events for your application.
*
* @return void
*/
public function boot()
{
parent::boot();
//
}
}
<?php
namespace App\Listeners;
use App\Events;
use Request;
use Illuminate\Auth\Events as LaravelEvents;
use Illuminate\Support\Facades\Log;
class LogActivity
{
public function login(LaravelEvents\Login $event)
{
$ip = \Request::getClientIp(true);
$this->info($event, "User {$event->user->email} logged in from {$ip}", $event->user->only('id', 'email'));
}
public function logout(LaravelEvents\Logout $event)
{
$ip = \Request::getClientIp(true);
$this->info($event, "User {$event->user->email} logged out from {$ip}", $event->user->only('id', 'email'));
}
public function registered(LaravelEvents\Registered $event)
{
$ip = \Request::getClientIp(true);
$this->info($event, "User registered: {$event->user->email} from {$ip}");
}
public function failed(LaravelEvents\Failed $event)
{
$ip = \Request::getClientIp(true);
$this->info($event, "User {$event->credentials['email']} login failed from {$ip}", ['email' => $event->credentials['email']]);
}
public function passwordReset(LaravelEvents\PasswordReset $event)
{
$ip = \Request::getClientIp(true);
$this->info($event, "User {$event->user->email} password reset from {$ip}", $event->user->only('id', 'email'));
}
protected function info(object $event, string $message, array $context = [])
{
//$class = class_basename($event::class);
$class = get_class($event);
Log::info("[{$class}] {$message}", $context);
}
}
@marventhieme
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment