Skip to content

Instantly share code, notes, and snippets.

@eusonlito
Created October 17, 2020 15:18
Show Gist options
  • Save eusonlito/773bd0ef3de74ba998e0fb6f8cc42d9a to your computer and use it in GitHub Desktop.
Save eusonlito/773bd0ef3de74ba998e0fb6f8cc42d9a to your computer and use it in GitHub Desktop.
Laravel Mail Logger
<?php declare(strict_types=1);
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\Services\Mail\Logger as LoggerMail;
class Debug extends ServiceProvider
{
/**
* @return void
*/
public function boot()
{
$this->logging();
}
/**
* @return void
*/
protected function logging(): void
{
$this->loggingMail();
}
/**
* @return void
*/
protected function loggingMail(): void
{
if (config('logging.channels.mail.enabled')) {
LoggerMail::listen();
}
}
}
<?php declare(strict_types=1);
namespace App\Services\Mail;
use Illuminate\Mail\Events\MessageSending;
use Illuminate\Support\Facades\Event;
class Logger
{
/**
* @return void
*/
public static function listen(): void
{
Event::listen(MessageSending::class, static fn ($event) => static::store($event));
}
/**
* @param \Illuminate\Mail\Events\MessageSending $event
*
* @return void
*/
protected static function store(MessageSending $event): void
{
$file = storage_path('logs/mails/'.date('Y-m-d/H-i-s').'-'.uniqid().'.log');
$dir = dirname($file);
if (is_dir($dir) === false) {
mkdir($dir, 0755, true);
}
file_put_contents($file, (string)$event->message, LOCK_EX);
}
}
<?php
return [
'providers' => [
/*
* Application Service Providers...
*/
App\Providers\Debug::class,
],
];
<?php
return [
'channels' => [
'mail' => [
'enabled' => env('LOG_MAIL', false),
],
],
];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment