Mity Docs Mity Docs for Mity Digital
Logger for Statamic Documentation

Custom events

Logger for Statamic can listen for your own events - or extend existing listeners - to help you flesh out Logger's capabilities.

If you are happy with the out-of-the-box functionality, you can skip this section. This is aimed at developers to help them understand how Logger for Statamic can be extended.

Before you get coding, give this a read to understand how to configure and extend Logger for Statamic for your own events.

A few little tidbits to help:

  • log messages are actually arrays, JSON-encoded, so more complex data can be stored

  • all messages have an accompanying view - just a standard blade template - that is used to render your log message to the user. This means you have full control over how the log message data appears

  • the log message automatically includes the following data - you won't need to take care of it yourself:

    • user details of who triggered the event, if known

    • the listener responsible

    • the event being handled

    • an array of data

    • and of course the date and time

Still with me? Let's go!

The Event Listener

When an event is fired, it is handled by an event listener.

All of Logger for Statamic's listeners extend the base EventListener class, which includes behaviour to:

  • write to the log

  • get the appropriate data to log

  • get supplementary data

  • get the view, action (verb) in plain human language, and type

Your event listeners for Logger for Statamic should extend this base class - it will make your life easy for creating your own listeners without having to write much code.

You'll either create a new event listener (for your own events) or extend an existing event listener (for existing events).

Create a new Event Listener

You'll do this when you're wanting to listen to a brand new event, or do something fancy.

Check out the docs on creating a new event listener for more details.

Extend an existing Event Listener

You'll want to do this if you want to log something specific for an existing event beyond what is already configured.

Check out the docs for extending an existing event listener for more details.

Create your views

The views are used by the reader side of Logger for Statamic, and translates your log's data in to a human readable format. This means that you can use language that relates to the user, including details that are aimed at non-tech users.

Maybe you have custom fields you want to show... or want to log before and after states... or include more advanced details. That's up to you! And you have Blade's tools at your disposal.

Your views will receive

  • a $data object that includes the stored data from the log entry

  • the $event that triggered it

  • an instance of the $handler that handled the event

Subscribe in your Event Service Provider

Now that the hard work is done, you need to subscribe to your event. This can be done by calling Logger for Statamic's subscribe facade method.

See the code examples in the Subscribing to events docs.