Mity Docs Mity Docs for Mity Digital
Logger for Statamic Documentation

Event Listener

Event Listener's in Logger for Statamic all extend the special \MityDigital\StatamicLogger\Abstracts\EventListener class.

You can view its source on Github.

This class includes a number of abstract methods that every listener must implement, and other methods that you can extend if you need, or simply use their default implementations.

Required methods

These methods are abstract within Logger's Event Listener base class, and you must write your own implementations if you're creating a new listener.

These are always required in every listener.

data(mixed $event): array

Accepts an instance of your event.

Returns an array of data that will be added to the log message.

verb(mixed $event): string

Accepts an instance of your event.

Returns a string of the verb for the event type. For example, if your event is "ModelCreated", your verb would be "created".

If your listener is used by multiple events - which Logger for Statamic's built-in listeners do - you can include a match, switch or condition to return a verb suitable to the event. This means that you can have a single listener for all of your model types - ModelCreated, ModelSaved, ModelSaving, ModelDeleted, etc - and have the verb method return an appropriate verb for each event type.

view(): string

Returns a string that represents the Blade view for the listener.

This will be directly passed to Laravel's view() helper.

See Customising views for more details.

Additional methods

These methods are implemented within the base event listener, but are designed for you to override in your own listener if you need.

supplement(mixed $event): array

Used if you are extending an existing listener (that is built in by Logger for Statamic) and simply want to add additional data to the log.

Accepts an instance of your event.

Returns an array of supplemental data that will be merged with the listener's data method array.

type(): string

Returns a string representing the type of model/object you're working with.

This will appear in Logger for Statamic's "type" column when viewing logs, and can be useful to help group log types together.

For example, any of the Asset-related listeners will return the word "Asset". Entry-related listeners return "Entry".

If you had a "Comment" model, you may like to return "Comment, or "User Comment" - whatever makes most sense for your app.

Optional methods

These methods are fully implemented within the base listener, and you don't need to override these unless you want to change their default behaviour.

action(): string

Returns a string of what the event is doing, and utilises the verb method.

If a verb is not found (such as an invalid match), the event name will be returned, or the handler name if there is no event set.

To set an event, you would call the setActionEvent method - however this is used internally only so there's no need for you to need this.

buildLogEntry(mixed $event): array

Accepts your event.

Prepares the data to be written to the log.

You should never need to override this function: the data method returns the listener's base data, and supplement can add additional data. These should give you enough additional data support.

handle(mixed $event): void

Accepts an instance of your event.

By default, writes to the log.

You may want to override this if you are creating a specific listener that should not write to the log, but may need to do something else - perhaps store temporary data, such as the "before" state of the model being saved.

Calling parent::handle($event) from within your implementation will write to the log.

Omitting this will not write anything to the log.