Skip to main content

Logs

Qore includes Spatie Activity Log for logging. The model used for these logs are \Qore\System\Models\Tenant\Activity::class.

This model appends the users' active session by default.

See https://spatie.be/docs/laravel-activitylog for full documentation.

Getting started

By default, every update, create, delete and resource actions on resources are logged by default.

Whether something should be logged and how it should be logged is defined on each field.

danger

Every field is logged by default, so you should be careful with sensitive data.

Disable logging

You can disable logging for a field:

Text::make(__('Password'), 'password')
->shouldLog(false)

Or conditionally:

Text::make(__('My field'), 'field')
->shouldLog(function(Model $new, ?Model $old = null) {
// Only log if we're creating the resource or the new value is not identical
return is_null($old) || $new->{$this->name} !== $old->{$this->name};
});

Specify a log display value

When a customer wants to see logs, it may be a good idea to display something clean and readable.

You can define a display value for your logs:

DateTime::make(__('My field'), 'field')
->logDisplayUsing(function ($value) {
if ($carbon = Carbon::parse($value)) {
return date_time_presenter($carbon);
}

return null;
});

Determine what should be logged

Fields that fill anything other than strings and numbers may clutter your database with large objects or arrays.

You can define what should be logged:

MyCustomField::make(__('My field'), 'field')
->logUsing(function (Model $new, ?Model $old = null) {
// Only log the label of the json value
return $new->{$this->name}['label'];
});

Logging models

In some cases you don't want to manage a model via a QoreResource, but logs should still be created.

You can still use the Spatie LogsActivity trait on models:

class SomeModel extends Model
{
use LogsActivity;

/**
* @var string[]
*/
protected static $logAttributes = ['name', 'email'];

/**
* @var bool
*/
protected static $logOnlyDirty = true;

/**
* @var bool
*/
protected static $submitEmptyLogs = false;
}
info

To prevent overlap with resource logging, you may want to disable logging on (some of) your fields.

Manual logging

You can manually create activity logs:

activity()
->performedOn($model)
->by(auth()->user())
->withProperty('id', $id)
->log($description);

If you want a custom note in your table and detail dialog you can add the 'note' property:

activity()
->performedOn($model)
->by(auth()->user())
->withProperties([
'id' => $id,
'note' => $note
])
->log($description);