Installation &amp; setup | laravel-notification-log | Spatie

 SPATIE

  Laravel Notification Log
===========================

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-notification-log](https://spatie.be/docs/laravel-notification-log/v1)  Installation &amp; setup

 Version   v1

 Other versions for crawler [v1](https://spatie.be/docs/laravel-notification-log/v1)

- [ Introduction ](https://spatie.be/docs/laravel-notification-log/v1/introduction)
- [ Support us ](https://spatie.be/docs/laravel-notification-log/v1/support-us)
- [ Requirements ](https://spatie.be/docs/laravel-notification-log/v1/requirements)
- [ Installation &amp; setup ](https://spatie.be/docs/laravel-notification-log/v1/installation-setup)
- [ Questions and issues ](https://spatie.be/docs/laravel-notification-log/v1/questions-issues)
- [ Changelog ](https://spatie.be/docs/laravel-notification-log/v1/changelog)
- [ About us ](https://spatie.be/docs/laravel-notification-log/v1/about-us)

Basic usage
-----------

- [ Getting started ](https://spatie.be/docs/laravel-notification-log/v1/basic-usage/getting-started)
- [ Determining which notifications get logged ](https://spatie.be/docs/laravel-notification-log/v1/basic-usage/determining-which-notifications-get-logged)
- [ Adding extra info to a logged notifications ](https://spatie.be/docs/laravel-notification-log/v1/basic-usage/adding-extra-info-to-your-notification)
- [ Querying the notification log ](https://spatie.be/docs/laravel-notification-log/v1/basic-usage/querying-the-notification-log)
- [ Working with fingerprints ](https://spatie.be/docs/laravel-notification-log/v1/basic-usage/working-with-fingerprints)

Advanced usage
--------------

- [ Customizing notification types ](https://spatie.be/docs/laravel-notification-log/v1/advanced-usage/customizing-notification-types)
- [ Customizing the logging process ](https://spatie.be/docs/laravel-notification-log/v1/advanced-usage/customizing-the-logging-process)
- [ Handling on demand notifications ](https://spatie.be/docs/laravel-notification-log/v1/advanced-usage/handling-on-demand-notifications)
- [ Using your own notification log model ](https://spatie.be/docs/laravel-notification-log/v1/advanced-usage/using-your-own-notification-log-model)

 Installation &amp; setup
========================

You can install the package via composer:

```
composer require spatie/laravel-notification-log
```

You can publish and run the migrations with:

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

### Publishing the config file

Optionally, you can publish the config file with:

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

This is the contents of the published config file:

```
return [
    /*
     * This model will be used to log all sent notifications
     */
    'model' => Spatie\NotificationLog\Models\NotificationLogItem::class,

    /*
     * Log items older than this number of days will be automatically be removed.
     *
     * This feature uses Laravel's native pruning feature:
     * https://laravel.com/docs/12.x/eloquent#pruning-models
     */
    'prune_after_days' => 30,

    /*
     * If this is set to true, any notification that does not have a
     * `shouldLog` method will be logged.
     */
    'log_all_by_default' => config('notification-log.log_all_by_default'),

    /*
     * By overriding these actions, you can make low level customizations. You can replace
     * these classes by a class of your own that extends the original.
     *
     */
    'actions' => [
        'convertEventToModel' => Spatie\NotificationLog\Actions\ConvertNotificationSendingEventToLogItemAction::class
    ],

    /*
     * The event subscriber that will listen for the notification events fire by Laravel.
     * In most cases, you don't need to touch this. You could replace this by
     * a class of your own that extends the original.
     */
    'event_subscriber' => Spatie\NotificationLog\NotificationEventSubscriber::class,
];
```

### Pruning results

This package will store all sent notifications in the `notification_log_items` table. The related `NotificationLogItems` models uses the [Laravel's `MassPrunable` trait](https://laravel.com/docs/12.x/eloquent#mass-pruning). In the `notification-log` config file, you can specify the maximum age of a model in the `prune_after_days` key. Don't forget to schedule the `model:prune` command, as instructed in Laravel's docs. You'll have to explicitly add the model class:

#### Laravel 11+

```
// in routes/console.php

use Illuminate\Support\Facades\Schedule;

Schedule::command('model:prune', [
    '--model' => [
        \Spatie\NotificationLog\Models\NotificationLogItem::class,
    ],
])->daily();
```

#### Laravel 10

```
// in app/Console/Kernel.php

class Kernel extends ConsoleKernel
{
    protected function schedule(Schedule $schedule)
    {
         $schedule->command('model:prune', [
                    '--model' => [
                        \Spatie\NotificationLog\Models\NotificationLogItem::class,
                    ],
        ])->daily();

        // ...
    }
}
```
