Mity Docs Mity Docs for Mity Digital
Logger for Statamic Documentation

Temporary data

Logger for Statamic has built-in support for storing temporary data during your app's request.

This is to help you manage state changes between different events - such as temporarily storing the state of a User before it is saved, and comparing it after it has been saved to correctly log the \Statamic\Users\UserSaved event after a successful save.

If you're writing your own (or extending) an event listener, you could override the handle method to store some temporary data for use later. Don't forget to check out the Event Listener docs for full details.

addTemporaryData(string $key, array $data): void

You can add data by calling the addTemporaryData method on the Logger for Statamic facade (use MityDigital\StatamicLogger\Facades\StatamicLogger).

Your $key is a unique key that you will need to use when getting data later - so make sure you have access to all of the details that make up this unique key in your handler for the get process too.

The $data is an array of data to temporarily store. This can be whatever you want or need - but must be an array.

1namespace MityDigital\StatamicLogger\Facades\StatamicLogger;
2 
3class UserSavingListener extends \MityDigital\StatamicLogger\Listeners\User
4{
5 public function handle(mixed $event): void
6 {
7 // get the current user from the store
8 $user = User::find($event->user->id);
9 
10 // temporarily keep the data
11 StatamicLogger::addTemporaryData('User-'.$user->id, $user->toArray());
12 }
13}

This will add the User's data to the temporary store, with the key of User-1 (assuming the user's ID is 1), and is their state before the saving process takes place.

After the request ends, all temporary data is lost. It only hangs around for the duration of the request.

getTemporaryData(string $key): ?array

You can retrieve temporary data by calling the getTemporaryData method on the Logger for Statamic facade (use MityDigital\StatamicLogger\Facades\StatamicLogger).

This requires the same $key that you used to add the temporary data, and will return an array of data, or null if the $key was not found.

1namespace MityDigital\StatamicLogger\Facades\StatamicLogger;
2 
3class UserSavedListener extends \MityDigital\StatamicLogger\Listeners\User
4{
5 public function handle(mixed $event): void
6 {
7 // get the current user from the store
8 $oldUser = StatamicLogger::getTemporaryData('User-'.$event->user->id);
9 
10 // do something with the old data, or even call parent::handle($event) to log something
11 }
12}