Files
core/src/Logging/ActivityLogService.php
T
2026-07-01 18:35:39 +03:00

64 lines
1.9 KiB
PHP

<?php
namespace Modules\Core\Logging;
use Illuminate\Database\Eloquent\Model;
/**
* Thin wrapper around Spatie Activity Log that standardises the log channel,
* actor (authenticated staff member), and property shape for all domain events.
*
* All logs are written to the 'lunar' channel. The subject is always an
* Eloquent model, and the actor is resolved from the 'staff' guard at call time.
*/
class ActivityLogService
{
/**
* Log a creation event. $attributes describes the initial state.
*/
public function created(Model $subject, array $attributes): void
{
activity('lunar')
->performedOn($subject)
->causedBy(auth('staff')->user())
->withProperties(['attributes' => $attributes])
->log('created');
}
/**
* Log an update event. $old holds the previous values, $attributes the new ones.
*/
public function updated(Model $subject, array $old, array $attributes): void
{
activity('lunar')
->performedOn($subject)
->causedBy(auth('staff')->user())
->withProperties(['old' => $old, 'attributes' => $attributes])
->log('updated');
}
/**
* Log a failed operation. $attributes provides context (e.g. error message, service).
*/
public function failed(Model $subject, array $attributes): void
{
activity('lunar')
->performedOn($subject)
->causedBy(auth('staff')->user())
->withProperties(['attributes' => $attributes])
->log('failed');
}
/**
* Log a deletion event. $attributes provides context (e.g. reason, name).
*/
public function deleted(Model $subject, array $attributes): void
{
activity('lunar')
->performedOn($subject)
->causedBy(auth('staff')->user())
->withProperties(['attributes' => $attributes])
->log('deleted');
}
}