okaufmann/laravel-notification-log

Logs every sent Notification and Mail of your entire Project.

Installs: 15 656

Dependents: 0

Suggesters: 0

Security: 0

Stars: 4

Watchers: 0

Forks: 2

pkg:composer/okaufmann/laravel-notification-log


README

Latest Version on Packagist Tests PHPStan Check & fix styling Total Downloads

Logs every sent Notification of your entire Laravel Project.

Installation

You can install the package via composer:

composer require okaufmann/laravel-notification-log

You can publish and run the migrations with:

php artisan vendor:publish --tag="notification-log-migrations"
php artisan migrate

You can publish the config file with:

php artisan vendor:publish --tag="notification-log-config"

The following config file will be published in config/notification-log.php:

return [

    /*
    |--------------------------------------------------------------------------
    | Resolve Notification Message
    |--------------------------------------------------------------------------
    |
    | If this is enabled, the Logger will try to resolve the built message
    | out of the notification. This is useful if you want to debug your
    | sent notifications.
    |
    */

    'resolve_notification_message' => env('NOTIFICATION_LOG_RESOLVE_NOTIFICATION_MESSAGE', false),
];

Usage

Add the following Interface and Trait to your Notification:

use Okaufmann\LaravelNotificationLog\Contracts\ShouldLogNotification;use Okaufmann\LaravelNotificationLog\Models\Concerns\LogsNotifications;

class DummyNotification extends Notification implements ShouldLogNotification
{
    use LogsNotifications;

    // ...

}

Now send a Notification or Mail as you would normally do. The package will automatically log the Notification or Mail.

Custom Message Resolution

If you want to customize how the notification message is resolved for logging purposes, you can implement the ResolveMessageForLogging interface:

use Okaufmann\LaravelNotificationLog\Contracts\ShouldLogNotification;
use Okaufmann\LaravelNotificationLog\Contracts\ResolveMessageForLogging;
use Okaufmann\LaravelNotificationLog\Models\Concerns\LogsNotifications;

class CustomNotification extends Notification implements ShouldLogNotification, ResolveMessageForLogging
{
    use LogsNotifications;

    public function resolveMessageForLogging(string $channel, $notifiable): string
    {
        $channelName = get_class($channel);
        $notifiableName = get_class($notifiable);

        return "Custom message for {$channelName} channel sent to {$notifiableName}";
    }
}

When a notification implements ResolveMessageForLogging, the logger will use your custom method instead of the default message resolution logic. This gives you full control over what gets stored in the message field of the notification log.

Resolving Messages After Sending

For special channels like WhatsApp, Telegram, or other messaging services where templates are stored externally and the actual message content is only available after sending, you can implement the ResolveMessageForLoggingAfterSent interface:

use Okaufmann\LaravelNotificationLog\Contracts\ShouldLogNotification;
use Okaufmann\LaravelNotificationLog\Contracts\ResolveMessageForLoggingAfterSent;
use Okaufmann\LaravelNotificationLog\Models\Concerns\LogsNotifications;

class WhatsAppNotification extends Notification implements ShouldLogNotification, ResolveMessageForLoggingAfterSent
{
    use LogsNotifications;

    public function resolveMessageForLoggingAfterSent(mixed $channel, $notifiable, $response): ?string
    {
        // Extract the actual message content from the WhatsApp API response
        if (isset($response['message_id'])) {
            return "WhatsApp message sent with ID: {$response['message_id']}";
        }

        return null;
    }
}

This interface is particularly useful for channels where:

  • Templates are stored externally (like WhatsApp Business API)
  • The actual message content is only available after sending
  • You need to extract information from the channel's response data

The method is called during the NotificationSent event, allowing you to resolve the message content using the response data from the channel after the notification has been successfully sent.

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Credits

License

The MIT License (MIT). Please see License File for more information.