Mity Docs Mity Docs for Mity Digital
Logger for Statamic Documentation

Create your own event listeners

When creating a new event listener, you'll need to ensure your listener defines the data, view and verb methods. Every event listener needs these.

Here's an example for an imaginary model of type "Comment", that would get handled on a CommentCreated, CommentDeleted or CommentSaved event being fired:

1class Comment extends \MityDigital\StatamicLogger\Abstracts\EventListener
2{
3 protected function data($event): array
4 {
5 return [
6 'id' => $event->comment->id,
7 'comment' => $event->comment->comment,
8 'author' => $event->comment->author->name()
9 ];
10 }
11 
12 protected function verb($event): string
13 {
14 return match ($event) {
15 \App\Events\CommentCreated::class => __('statamic-logger::verbs.created'),
16 \App\Events\CommentDeleted::class => __('statamic-logger::verbs.deleted'),
17 \App\Events\CommentSaved::class => __('statamic-logger::verbs.saved')
18 };
19 }
20 
21 public function view(): string
22 {
23 return 'listeners.comment';
24 }
25}

Refer to the Event Listener docs and description for full details.

The data method returns an array of data to include in the log. You will receive the event as input, and can access its properties.

The verb method returns a past-tense verb for what the event was - such as "created" or "saved". These are helpfully stored in a language file - but you could always write your own language here. This is always retrieved at read-time, and isn't baked in to the log itself.

The view method returns the path to the view - your standard blade file type - that will be used to format the message when reading the log.

If you want to see examples, browse Logger for Statamic's source Listeners.