Description
In the same sense as #110, which extended details ability to hold much more useful information about the logged entry, I would like to propose another extension to this great package.
Imagine an activity table with tons of millions records. A person requires to audit activity records for a particular model, let's say app\Models\User
. That would be easy today as there is a userId
in the activity table, although not indexed to speed up queries.
But then this fictional person discovers some oddities to that user activity and decides to investigate further. She now wants to know all the app\Models\Post
activities that suspicious user have messed with. From now on things become much more harder within laravel-logger
.
My suggestion to address this use case:
- In the migration and model, add these two columns:
--- migration.php 2023-08-27 16:41:36.224353642 -0300
+++ migrationRelId.php 2023-08-27 16:42:44.115500134 -0300
@@ -32,6 +32,8 @@
$table->string('locale')->nullable();
$table->longText('referer')->nullable();
$table->string('methodType')->nullable();
+ $table->unsignedBigInteger('relId')->index()->nullable();
+ $table->string('relModel')->nullable();
$table->timestamps();
$table->softDeletes();
});
- Then in
jeremykenedy\LaravelLogger\App\Http\Traits\ActivityLogger
:
--- ActivityLogger.php 2023-08-27 16:39:29.755943565 -0300
+++ ActivityLoggerRelId.php 2023-08-27 17:00:39.381982204 -0300
@@ -15,10 +15,11 @@
*
* @param null $description
* @param null $details
+ * @param ?array $rel
*
* @return void
*/
- public static function activity($description = null, $details = null)
+ public static function activity($description = null, $details = null, $rel = null)
{
$userType = trans('LaravelLogger::laravel-logger.userTypes.guest');
$userId = null;
@@ -68,6 +69,14 @@
$ip = Request::ip();
}
+
+ $relId = null;
+ $relModel = null;
+ if (is_array($rel) && array_key_exists('id', $rel) && array_key_exists('model', $rel)) {
+ $relId = $rel['id'];
+ $relModel = $rel['model'];
+ }
+
$data = [
'description' => $description,
'details' => $details,
@@ -79,6 +88,8 @@
'locale' => Request::header('accept-language'),
'referer' => Request::header('referer'),
'methodType' => Request::method(),
+ 'relId' => $relId,
+ 'relModel' => $relModel,
];
// Validation Instance
@@ -113,6 +124,8 @@
'locale' => $data['locale'],
'referer' => $data['referer'],
'methodType' => $data['methodType'],
+ 'relId' => $data['relId'],
+ 'relModel' => $data['relModel'],
]);
}
There! All the basics to extend activity log functionality a lot is above. Of course this could be further improved with UI fields in the search page and such. Anyway, all the pieces to provide an easy way to relate any model to an activity model is there. This makes the job of anyone looking to track, obtain and get knowledge about every step on any particular model a breeze.
BONUS: anyone looking to create reports of a model activity could do so in a very fast way just by querying activity
table straight away, thanks to the indexed relId
column, e.g.:
SELECT userId, ipAddress, details FROM activity WHERE relId = 30 AND relModel = 'App\Models\Post';
As a side note, I realize there is the route
column that could be used to look for the above. But the fact it holds full application URL doesn't make it as a good candidate for indexing and lightning fast searches. The proposed method above delivers even in huge activities tables with billions records.
What would you say, @jeremykenedy?